Sei sulla pagina 1di 5

Date: - 06/02/2002 DIV: - A

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

following e ample of unsorted array to trance it in the selection sort form.

/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

The following 5etailed algorithm formali6es this process.

*Selection sort Algorithm.


Steps: 1: Algorithm selects $!a, n, k" 2: 77 Selects the kth smallest element in a 8$. n9 and :laces it in the kth 77 :osition of a 89. The remaining elements are rearranged such that 77 a8m9 ;< a8k9. 3: 77 for $ ;< m ; k and a8m9 =< a8k9 for k ; m ;< n. 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: } { low. < $> up. < n?$> a 8n?$9.< 2> 77 a 8n?$9 is set to infinity repeat @ // Each time the loop is entered, $;< low;< up ;< n?$> A. < partion !a, low, up"> 77 A is such that a8A9 is the Ath smallest #alue in a 89. if !k < A" then return> else if !k ; A" then up. < A> 77 A is the new upper limit. else low. < A?$> 77 A?$ is the new lower limit. } until !false">

Secection Sort Program.


/* This pr !ra" #i$$ !i%e & ' s rti(! 'si(! S)*)+TI,S,.T /eth 0 */
/* Selection sorting program. */ /* Includes the header files */
#include<stdio.h> #include<conio.h> #include<iostream.h> void main () { Clrscr ();

/* 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

/* Input elements or values of array */


3ive no4&;

/* 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

/* To display all sorted numbers in ascending order*/


cout <<&'n't Sorted num:ers in ascending order4 'n&; 3or (i!5; i<"; i66) { cout <<&'n *he no is....&<<a#i$; 7 getch ();

* +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

Potrebbero piacerti anche