DS (RM) Program - Selection Sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include<iostream.h> #include<conio.h> #define SIZE 10 void main(void) { int L[SIZE]; int i,n=0,current,small,walk,temp; clrscr(); cout<<"Input Number of elements to be sorted : "; cin>>n; cout<<"Input unordered list of elements to be sorted\n"; for(i=0; i<n; i++) cin>>L[i]; current = 0; while(current < n) { small = current; walk = current + 1; while(walk <= n) { if(L[walk] < L[small]) small = walk; walk+=1; } temp=L[current]; L[current] = L[small]; L[small] = temp; current++; } cout<<"Sorted element by selection sort method : \n"; for(i=0; i<n; i++) cout<<"\t"<<L[i]; getch(); } |
Input Number of elements to be sorted : 5 Input unordered list of elements to be sorted 23 66 13 32 8
Sorted element by Selection Sort Method : 8 13 23 32 66
