Algorithms CSCI 235, Spring 2019 Lecture 13 Elementary Sorts II
Insertion Sort Idea: Sort items one at a time into previously sorted group. Invariant: After the ith step of the algorithm, A[1..i] is sorted.
Insertion sort algorithm for i = 2 to A.length Insert(A, i) Insert(A, i) key = A[i] j = i while j > 1 and less(key, A[j-1]) A[j] = A[j-1] j = j-1 // Insert key A[j] = key
Is it Stable and in place? Stepping through the insertion sort algorithm: A a b c d 2 1 2 1 1 2 3 4 Example: Array already sorted alphabetically. Sort by numbers as key. If the algorithm is stable, the letters associated with the same number will stay in order relative to one another. Index: We will step through the algorithm in class.
Running time of Insertion Sort Already showed that worst case running time is Q(n2) This is true for both the recursive version and for the iterative version (as derived earlier in class). Insertion-Sort(A) for i = 2 to A.length Insert(A, i) Array size to be solved decreases by 1 each time through loop. T(n) = T(n-1) + cost of Insert Insert(A, i) key = A[i] j = i while j > 1 and less(key, A[j-1]) A[j] <- A[j-1] j <- j-1 {Insert key} A[j] = key Array size to be solved decreases by 1 each time through loop. T2(n) = T2(n-1) + cost of assignments = T2(n-1) + 1 T2(n) = Q(n) T(n) = T(n-1) + n T(n) = Q(n2)
Selection Sort Idea: On the ith step of the algorithm, store into A[i] the smallest element of A[i..n]. Invariant: After step i, the elements A[1..i] are in their final sorted positions.
Selection sort algorithm for i = 1 to A.length - 1 swap(A, i, Min-Index(A, i, A.length)) Min-Index(A, lo, hi) // Assume lo and hi are legal subscripts, and hi >= lo. min_index = lo for i = lo + 1 to hi if less(A[i], A[min_index]) min-index = i return min_index Is Selection sort stable? Is it in place? We will work through an example in class.
Stability of an algorithm To show that an algorithm is not stable, it is sufficient to find an example for which the relative order of elements is not preserved during the sort. To show that an algorithm is stable may take more reasoning, because an unstable algorithm may preserve the relative order of elements for some initial orderings of an array but not others.
Running time of Selection Sort Selection-Sort(A) for i = 1 to A.length - 1 swap(A, i, Min-Index(A, i, A.length)) Min-Index(A, lo, hi) // Assume lo and hi are legal subscripts, and hi >= lo. min_index = lo for i = lo + 1 to hi if less(A[i], A[min_index]) min-index = i return min_index T(n) = T(n-1) + cost of Min-Index T2(n) = T2(n-1) + cost of assignments and less( ) = T2(n-1) + 1 T2(n) = Q(n) T(n) = T(n-1) + n T(n) = Q(n2)
Bubble Sort Idea: On the every step of the algorithm, scan A from left to right and exchange adjacent elements that are out of order. Repeat until a scan finds no elements out of order. Invariant: After step i, (at least) the elements A[(n + 1 - i)..n] are in their final sorted positions.
Bubble sort pseudo-code Bubble-Sort(A) hi = length[A] changed = false repeat changed = Bubble-Up(A, 1, hi) hi = hi - 1 until not changed; Bubble-Up(A, lo, hi) for i = lo to hi - 1 if less(A[i + 1], A[i]) swap(A, i, i+1); changed = true return changed
Behavior of Bubble sort Is Bubble Sort stable? Try sorting: a b c d 2 1 2 1 Is Bubble Sort in place?
Running time of Bubble sort Bubble-Sort(A) hi = A.length changed = false repeat changed = Bubble-Up(A, 1, hi) hi = hi - 1 until not changed; Bubble-Up(A, lo, hi) for i = lo to hi - 1 if less(A[i + 1], A[i]) swap(A, i, i+1); changed = true return changed T(n) = ? T2(n) = ? T(n) = ?
Merge Sort Idea: Recursively sort subarrays and then merge them into a single sorted array.
Pseudo code for Merge Sort Merge-Sort(A) MSort(A, 1, A.length) MSort(A, lo, hi) if lo < hi mid = (lo + hi) div 2 MSort(A, lo, mid) MSort(A, mid + 1, hi) Merge(A, lo, mid, hi)
Merge pseudo-code Merge(A, lo, mid, hi) n = (hi - lo) + 1 // Merge elements into temporary array B. B =newArray(n) left = lo right <- mid + 1 for i = 1 to n if left ≤ mid and (right > hi or less(A[left], A[right])) B[i] = A[left] left = left + 1 else B[i] = A[right] right = right + 1 // Copy elements from B back to A A[left] = B[i]
Behavior of Merge Sort Is Merge Sort stable? Try sorting: a b c d 2 1 2 1 Is Merge Sort in place? Running Time of Merge Sort: Derived in previous lectures: T(n) = 2T(n/2) + Cost of Merge Cost of Merge: (2 loops, each with maximum size of n) T2(n) = 2n = Q(n) Merge sort: T(n) = 2T(n/2) + n T(n) = Q(nlgn)
Summary Algorithm Stable In place Running time Insertion Selection Bubble Merge