Basic sorting Selection sort Insertion sort September 24
Basic sorting Selection sort Insertion sort September 24, 2018 Cinda Heeren / Geoffrey Tien 1
Algorithm analysis – simple sorting Selection sort • September 24, 2018 Cinda Heeren / Geoffrey Tien 2
Selection sort 23 41 33 81 7 19 11 45 Find smallest unsorted item: 7 comparisons 7 41 33 81 23 19 11 45 Find smallest unsorted item: 6 comparisons 7 11 33 81 23 19 41 45 Find smallest unsorted item: 5 comparisons 7 11 19 81 23 33 41 45 Find smallest unsorted item: 4 comparisons 7 11 19 23 81 33 41 45 Find smallest unsorted item: 3 comparisons 7 11 19 23 33 81 41 45 Find smallest unsorted item: 2 comparisons 7 11 19 23 33 41 81 45 Find smallest unsorted item: 1 comparison 7 11 19 23 33 41 45 81 Sorted September 24, 2018 Cinda Heeren / Geoffrey Tien 3
Selection sort comparison operations September 24, 2018 Unsorted elements Comparisons … … 3 2 1 0 Cinda Heeren / Geoffrey Tien 4
Selection sort algorithm void Selection. Sort(int arr[], int size) { for (int i = 0; i < size -1; ++i) { int smallest = i; // Find the index of the smallest element for (int j = i + 1; j < size; ++j) { if (arr[j] < arr[smallest]) { smallest = j; } } // Swap the smallest with the current item temp = arr[i]; { arr[i] = arr[smallest]; arr[smallest] = temp; } } September 24, 2018 Cinda Heeren / Geoffrey Tien 5
Stability Definition • Stable September 24, 2018 Not stable Cinda Heeren / Geoffrey Tien 6
Stability of selection sort • Is Selection sort stable? 9 4 9 2 2 4 9 9 – No, but it can be made stable (at the expense of performing many more swaps) September 24, 2018 Cinda Heeren / Geoffrey Tien 7
Selection sort summary • September 24, 2018 Cinda Heeren / Geoffrey Tien 8
Insertion sort • Another simple sorting algorithm – Divides array into sorted and unsorted parts • The sorted part of the array is expanded one element at a time – Find the correct place in the sorted part to place the 1 st element of the unsorted part • By searching through all of the sorted elements – Move the elements after the insertion point up one position to make space – we insert the element into its correct place (among the items processed so far) September 24, 2018 Cinda Heeren / Geoffrey Tien 9
Insertion sort First element is already "sorted" 23 41 33 81 7 19 11 45 Locate position for 41 – 1 comparison 23 41 33 81 7 19 11 45 Locate position for 33 – 2 comparisons 23 33 41 81 7 19 11 45 Locate position for 81 – 1 comparison 23 33 41 81 7 19 11 45 Locate position for 7 – 4 comparisons 7 23 33 41 81 19 11 45 Locate position for 19 – 5 comparisons 7 19 23 33 41 81 11 45 Locate position for 11 – 6 comparisons 7 11 19 23 33 41 81 45 Locate position for 45 – 2 comparisons 7 11 19 23 33 41 45 81 Sorted September 24, 2018 Cinda Heeren / Geoffrey Tien 10
Insertion sort algorithm void Insertion. Sort(int arr[], int size) { for (int i = 1; i < size; ++i) { temp = arr[i]; int pos = i; // Shuffle up all sorted items > arr[i] while (pos > 0 && arr[pos - 1] > temp) { arr[pos] = arr[pos – 1]; pos--; } // Insert the current item arr[pos] = temp; } } September 24, 2018 Cinda Heeren / Geoffrey Tien Inner loop: how many times? 11
Insertion sort cost Sorted Elements 0 Worst-case Search Worst-case Shuffle 0 0 1 1 1 2 2 2 … … … September 24, 2018 Cinda Heeren / Geoffrey Tien 12
Insertion sort best case • September 24, 2018 Cinda Heeren / Geoffrey Tien 13
Insertion sort worst case • September 24, 2018 Cinda Heeren / Geoffrey Tien 14
Insertion sort average case • September 24, 2018 Cinda Heeren / Geoffrey Tien 15
Insertion sort summary • When do we use Insertion sort? – Insertion sort is a good choice when the data are nearly sorted (only a few elements out of place), or when the problem size is small (because it has low overhead) Name Best Average Worst Stable Selection sort challenging Insertion sort Yes September 24, 2018 Cinda Heeren / Geoffrey Tien Memory 16
Readings for this lesson • Carrano & Henry – Chapter 11. 1. 1 (Selection sort) – Chapter 11. 1. 3 (Insertion sort) • Next class – Epp, Chapter 5. 5 (Correctness with loop invariants) September 24, 2018 Cinda Heeren / Geoffrey Tien 17
- Slides: 17