Sorting
Selection sort Algorithm selection_sort(A,n) input: An array A of n integers---- A[0] to A[n-1] output: An updated array A with elements sorted in ascending order for i = 0 to n-2 small_pos i for j = i+1 to n-1 if A[j] < A[small_pos] small_pos j temp A[i] A[i] A[small_pos] A[small_pos] temp
Running time of selection_sort Worst case O(n2) why? Best case O(n2)
Insertion sort Algorithm insertion_sort(A,n) input: An array A of n integers output: An updated array A with elements sorted in ascending order for j = 1 to n-1 key A[j] i j-1 while i>=0 and A[i] > key A[i+1] A[i] i i-1 A[i+1] key
Running time of insertion_sort Worst case O(n2) when and why? Best case O(n)
Bubble sort Algorithm bubble_sort(A,n) input: An array A of n integers---- A[0] to A[n-1] output: An updated array A with elements sorted in ascending order for i = 0 to n-2 for j = n-1 down to i+1 if A[j] < A[j-1] swap A[j] with A[j-1]
Running time of bubble_sort Worst case O(n2) why? Best case O(n2)
Modified Bubble sort Algorithm bubble_sort2(A,n) input: An array A of n integers---- A[0] to A[n-1] output: An updated array A with elements sorted in ascending order repeat_flag true i 0 while (repeat_flag = true and i<n-1) repeat_flag false for j = n-1 down to i+1 if A[j] < A[j-1] swap A[j] with A[j-1] i i + 1
Running time of bubble_sort2 Worst case O(n2) when and why? Best case O(n)
Heapsort Binary heap Max heap Min heap Nearly complete binary tree Completely filled on all levels except possibly the lowest Max heap Every node in the tree has a key value higher than its children Min heap Every node in the tree has a key value lower than its children
heap
heap – parent/child
heapsort functions
Build heap
Building heap – Example (a)
Building heap – Example (b)
Building heap – Example (c)
Building heap – Example (d)
Building heap – Example (e)
Building heap – Example (f)
Heapsort Algorithm
Heapsort Example (a)
Heapsort Example (b)
Heapsort Example (c)
Heapsort Example (d)
Heapsort Example (e)
Heapsort Example (f)
Heapsort Example (g)
Heapsort Example (h)
Heapsort Example (h)
Heapsort Example (i)
Heapsort Example (j)
Merge sort
Merge
Divide and conquer Divide the problem into a number of subproblems that are smaller instances of the same problem. Conquer the subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve the subproblems in a straightforward manner. Combine the solutions to the subproblems into the solution for the original problem.
Quick sort Divide: Partition (rearrange) the array A[p..r] into two (possibly empty) subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is less than or equal to A[q], which is, in turn, less than or equal to each element of A[q+1..r] . Compute the index q as part of this partitioning procedure.
Quick sort Conquer: Sort the two subarrays A[p..q-1] and A[q+1..r] by recursive calls to quicksort. Combine: Because the subarrays are already sorted, no work is needed to combine them: the entire array A[p..r]is now sorted.
Quicksort
Partition
Quicksort example
Quicksort example
Quicksort example
Performance analysis of quicksort Worst case O(n2) when and why? Best case O(n lg n) Average case O(n lg n) why?
Balanced Partition
Randomized quicksort
Quicksort - Median of three
Counting sort
Radix sort
Radix sort example
Radix sort - Exercise 8.3-1 (CLRS) Using the previous exercise as a model, illustrate the operation of RADIX-SORT on the following list of English words: COW, DOG, SEA, RUG, ROW, MOB, BOX, TAB, BAR, EAR, TAR, DIG, BIG, TEA, NOW, FOX.
Bucket sort Bucket sort assumes that the input is drawn from a uniform distribution Bucket sort divides the interval [0,1) into n equal-sized subintevals, or buckets An auxiliary array B[0..n-1] of linked lists (buckets) is used.
Bucket sort example
Bucket sort
Comparison of sorting algorithms Sorting technique Best Case Worst case In-place? stable? insertion O(n) O(n2) yes bubble selection no heap O(n lg n) merge quick counting O(n+k) radix O(d(n+k) bucket
External sorting Multi-way merge sort