Sorting Algorithms Selection Bubble Insertion and Radix Sort

Sorting Algorithms Selection, Bubble, Insertion and Radix Sort

Worst-case vs. Average-case Analyses • An algorithm can require different times to solve different problems of the same size. • Worst-case analysis (find the maximum number of operations an algorithm can execute in all situations) – is easier to calculate and is more common • Average-case (enumerate all possible situations, find the time of each of the m possible cases, total and dive by m) – is harder to compute but yields a more realistic expected behavior
![Selection Sort values [ 0 ] 36 [1] 24 [2] 10 [3] 6 [4] Selection Sort values [ 0 ] 36 [1] 24 [2] 10 [3] 6 [4]](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-3.jpg)
Selection Sort values [ 0 ] 36 [1] 24 [2] 10 [3] 6 [4] 12 Divides the array into two parts: already sorted, and not yet sorted. On each pass, finds the smallest of the unsorted elements, and swap it into its correct place, thereby increasing the number of sorted elements by one.
![Selection Sort: Pass One values [ 0 ] 36 [1] 24 [2] 10 [3] Selection Sort: Pass One values [ 0 ] 36 [1] 24 [2] 10 [3]](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-4.jpg)
Selection Sort: Pass One values [ 0 ] 36 [1] 24 [2] 10 [3] 6 [4] 12 U N S O R T E D
![Selection Sort: End Pass One values [ 0 ] 6 [1] 24 [2] 10 Selection Sort: End Pass One values [ 0 ] 6 [1] 24 [2] 10](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-5.jpg)
Selection Sort: End Pass One values [ 0 ] 6 [1] 24 [2] 10 [3] [4] 36 12 SORTED U N S O R T E D
![Selection Sort: Pass Two values [ 0 ] 6 [1] 24 [2] 10 [3] Selection Sort: Pass Two values [ 0 ] 6 [1] 24 [2] 10 [3]](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-6.jpg)
Selection Sort: Pass Two values [ 0 ] 6 [1] 24 [2] 10 [3] [4] 36 12 SORTED U N S O R T E D
![Selection Sort: End Pass Two values [ 0 ] [1] 6 10 [2] 24 Selection Sort: End Pass Two values [ 0 ] [1] 6 10 [2] 24](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-7.jpg)
Selection Sort: End Pass Two values [ 0 ] [1] 6 10 [2] 24 [3] 36 [4] SORTED 12 U N S O R T E D
![Selection Sort: Pass Three values [ 0 ] [1] 6 10 [2] 24 [3] Selection Sort: Pass Three values [ 0 ] [1] 6 10 [2] 24 [3]](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-8.jpg)
Selection Sort: Pass Three values [ 0 ] [1] 6 10 [2] 24 [3] 36 [4] SORTED 12 U N S O R T E D
![Selection Sort: End Pass Three values [ 0 ] 6 [1] 10 [2] 12 Selection Sort: End Pass Three values [ 0 ] 6 [1] 10 [2] 12](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-9.jpg)
Selection Sort: End Pass Three values [ 0 ] 6 [1] 10 [2] 12 [3] 36 [4] 24 S O R T E D UNSORTED
![Selection Sort: Pass Four values [ 0 ] 6 [1] 10 [2] 12 [3] Selection Sort: Pass Four values [ 0 ] 6 [1] 10 [2] 12 [3]](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-10.jpg)
Selection Sort: Pass Four values [ 0 ] 6 [1] 10 [2] 12 [3] 36 [4] 24 S O R T E D UNSORTED
![Selection Sort: End Pass Four values [ 0 ] 6 [1] 10 [2] 12 Selection Sort: End Pass Four values [ 0 ] 6 [1] 10 [2] 12](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-11.jpg)
Selection Sort: End Pass Four values [ 0 ] 6 [1] 10 [2] 12 [3] 24 [4] 36 S O R T E D
![Selection Sort: How many comparisons? values [ 0 ] 6 4 compares for values[0] Selection Sort: How many comparisons? values [ 0 ] 6 4 compares for values[0]](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-12.jpg)
Selection Sort: How many comparisons? values [ 0 ] 6 4 compares for values[0] [1] 10 3 compares for values[1] [2] 12 2 compares for values[2] 24 1 compare for values[3] [4] 36 = 4 + 3 + 2 + 1

For selection sort in general • The number of comparisons when the array contains N elements is Sum = (N-1)+(N-2)+ …+2+1

Notice that. . . Sum = (N-1) + (N-2) +. . . + 2 + 1 + Sum = 1 + 2 +. . . + (N-2) + (N-1) 2* Sum = N +. . . + N 2 * Sum = N * (N-1)/2=. 5 N 2 -. 5 N • How many number of swaps? • O(N) • Therefore, the complexity is O(N 2)
![Code for Selection Sort void Selection. Sort (int values[], int num. Values) // Post: Code for Selection Sort void Selection. Sort (int values[], int num. Values) // Post:](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-15.jpg)
Code for Selection Sort void Selection. Sort (int values[], int num. Values) // Post: Sorts array values[0. . num. Values-1 ] // into ascending order by key { int end. Index = num. Values - 1 ; for (int current=0; current<end. Index; current++) Swap (values, current, Min. Index(values, current, end. Index)); }
![Selection Sort code (contd) int Min. Index(int values[ ], int start, int end) // Selection Sort code (contd) int Min. Index(int values[ ], int start, int end) //](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-16.jpg)
Selection Sort code (contd) int Min. Index(int values[ ], int start, int end) // Post: Function value = index of the smallest value // in values [start]. . values [end]. { int index. Of. Min = start ; for(int index =start + 1 ; index <= end ; index++) if (values[ index] < values [index. Of. Min]) index. Of. Min = index ; return index. Of. Min; }

Sorting Algorithms Bubble, Insertion and Radix Sort
![Bubble Sort values [ 0 ] 36 [1] 24 [2] 10 [3] [4] 6 Bubble Sort values [ 0 ] 36 [1] 24 [2] 10 [3] [4] 6](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-18.jpg)
Bubble Sort values [ 0 ] 36 [1] 24 [2] 10 [3] [4] 6 12 Compares neighboring pairs of array elements, starting with the last array element, and swaps neighbors whenever they are not in correct order. On each pass, this causes the smallest element to “bubble up” to its correct place in the array.

Bubble Sort: Pass One 36 36 36 6 24 24 6 36 10 6 24 24 6 10 10 10 12 12

Bubble Sort: Pass Two 6 6 6 36 36 10 24 10 36 10 24 24 12 12 12

Snapshot of Bubble. Sort
![Code for Bubble Sort void Bubble. Sort(int values[], int num. Values) { int current Code for Bubble Sort void Bubble. Sort(int values[], int num. Values) { int current](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-22.jpg)
Code for Bubble Sort void Bubble. Sort(int values[], int num. Values) { int current = 0; while (current < num. Values - 1) { Bubble. Up(values, current, num. Values-1); current++; } }
![Bubble Sort code (contd. ) void Bubble. Up(int values[], int start. Index, int end. Bubble Sort code (contd. ) void Bubble. Up(int values[], int start. Index, int end.](http://slidetodoc.com/presentation_image_h2/b1fb3b38a1ef4ded0668c2d43c35efab/image-23.jpg)
Bubble Sort code (contd. ) void Bubble. Up(int values[], int start. Index, int end. Index) // Post: Adjacent pairs that are out of // order have been switched between // values[start. Index]. . values[end. Index] // beginning at values[end. Index]. { for (int index = end. Index; index > start. Index; index--) if (values[index] < values[index-1]) Swap(values, index-1); }

Observations on Bubble Sort • There can be a large number of intermediate swaps. • Can this algorithm be improved? – What are the best/worst cases? • This algorithm is O(N 2) in the worst-case
- Slides: 24