Selection SortThe selection sort enhances the bubble sort by making only a single swap for each pass through the rundown. Indorder to do this, a selection sort searches for the biggest value as it makes a pass and, after finishing the pass, places it in the best possible area. Similarly as with a bubble sort, after the first pass, the biggest item is in the right place. After the second pass, the following biggest is set up. This procedure proceeds and requires n1 goes to sort n item, since the last item must be set up after the (n1) st pass. ALGORITHM: SELECTION SORT (A) 1. k ← length [A] 2. for j ←1 to n1 3. smallest ← j 4. for I ← j + 1 to k 5. if A [i] < A [ smallest] 6. then smallest ← i 7. exchange (A [j], A [smallest]) Analysis:
In pass 1: n1 comparisons are required In pass 2: n2 comparisons are required In pass 3: n3 comparisons are required ............................................................................ ............................................................................... In pass n1: 1 comparison is required Total comparisons: T (n) = (n1) + (n2) + (n3) +........+ 1 = = o (n^{2}) Therefore complexity is of order n^{2} Example:Sort the following array using selection sort: A [] = (7, 4, 3, 6, 5). A [] =
1 Iteration:Swap 7 and 3
2^{nd} iteration:No Swap
3^{rd} iteration:Swap 7 and 5
4^{th} iteration:No Swap
Finally, the sorted list is:
Next TopicInsertion Sort
