Selection Sort
Selection Sort
prepared By :
viral italiya.
Sandip koshiya
Selection Sort
One of the easiest ways to sort a table is by selection. Beginning with the first record in the table, a search is performed to locate the element which has the smallest key. When this element is found, it is interchanged with the first record in the table. This interchange places the record with the smallest key in the first position of the table. A search for the second smallest keys of the records from the second element on word .The Element which has the second smallest key is interchanged with the element located in the second position of the table. The process of searching for the record with the ne t smallest key and placing it in its proper position !within the desired ordering" continues until all records ha#e been sorted in ascending order. A general algorithm for the selection sort is now presented. $. %epeat thru step & a total of n'$ times. (. %ecord the position of the #ector already sorted. ). %epeat step * for the elements in the unsorted portion of the #ector. *. %ecord location of smallest element in unsorted #ector. &. E change first element in unsorted #ector with smallest element. +n this method, in the first pass the location of the smallest element is found out by making a search from the first element. Then smallest element so on interchanged with the first element. Then in the second pass, the location of the second smallest element is interchanged with the smallest element of the table. So at the end of the second pass, second smallest element is placed in its proper position and so on the process of finding the ne t smallest element and then interchanging is continued until all elements are sorted. ,et-s see the
/nsorted array list. ** )) $$ && 00 12 Step $. ** )) $$ && 00 12 *2 32 11 (( 44 33 At end of Step $. $$ )) ** && 00 12 *2 32 11 (( 44 33 At end of Step (. $$ (( ** && 00 12 *2 32 11 )) 44 33 At end of Step ). $$ (( )) && 00 12 *2 32 11 ** 44 33 At end of Step *. $$ (( )) *2 00 12 && 32 11 ** 44 33 At end of Step &. $$ (( )) *2 ** 12 && 32 11 00 44 33 At end of Step 3. $$ (( )) *2 ** && 12 32 11 00 44 33 At end of Step 0. $$ (( )) *2 ** && 32 12 11 00 44 33 At end of Step 4. $$ (( )) *2 ** && 32 33 11 00 44 12 At end of Step 1. $$ (( )) *2 ** && 32 33 00 11 44 12 At end of Step $2. $$ (( )) *2 ** && 32 33 00 44 11 12 At end of Step $$. $$ (( )) *2 ** && 32 33 00 44 12 11
/* Variable declaration */
int large, ind, , n!", a#"$, i, %; cout<<&'n'n't ******* S()(C*+,- S,.*+-/ 0.,/.12 ****** 'n&; cout <<&'n (nter an 3or (i!5; i<"; i66) { cin >>a#i$; 7
/* for loop for comparing two elements and arrange in sorted form */
3or (i!n89 ;i>5 ;i88) { large ! a#5$;// define large = a[0] ind ! 5; // define ind = 0 3or (%!9; %<!i; %66) { i3 (a#%$>large) { large ! a#%$; ind ! %; 7 7 a#ind$ ! a#i$; // Interchange the elements of a[i] a#i$ ! large; // to large 7
* +3 ;e run a:ove program as inputting 3ollo;ing data, then ;e get output as given :elo;4
Output:Enter fi#e no. 4 & ( * $ Sorted numbers in ascending order. The no isB.$ The no isB.( The no is.B* The no is.B& The no is.B4