Sorting: - Dan Barrish-Flood
Sorting: - Dan Barrish-Flood
Dan Barrish-Flood
heapsort
made file 3-Sorting-Intro-Heapsort.ppt
Quicksort
Worst-case running time is (n2) on an input array of n numbers. Expected running time is (nlgn). Constants hidden by are quite small. Sorts in place. Probably best sorting algorithm for large input arrays. Maybe.
3
How does Quicksort work? based on divide and conquer paradigm (so is merge sort). Divide: Partition (re-arrange) the array A[p..r] into two (possibly empty) sub-arrays A[p .. q-1] and A[q+1 .. r] such that each element of A[p .. q-1] is each element of A[q], which is, in turn, each element of A[q+1 .. r]. Compute the index q as part of this partitioning procedure. Conquer: Sort the two sub-arrays A[p .. q-1] and A[q+1 .. r] by recursive calls to quicksort. Combine: No combining needed; the entire array A[p .. r] is now sorted!
4
Quicksort
Partition in action
Quicksort, best-case
In the most even possible split, PARTITION yields two subproblems each of size no more than n/2, since one is of size floor(n/2), and one is [ceiling(n/2)]-1. We get this recurrence, with some OK sloppiness: T(n) = 2T(n/2) + n (look familiar?) T(n) = O(nlgn) This is asymptotically superior to worstcase, but this ideal scenario is not likely...
8
Quicksort, Average-case
suppose the great and awful splits alternate levels in the tree. the running time for QS, when levels alternate between great and awful splits, is just the same as when all levels yield great splits! (with a slightly larger constant hidden by the big-oh notation). So, average case... T(n) = O(nlgn)
9
We will show that any sorting algorithm based only on comparison of the input values must run in (nlgn) time.
10
Decision Trees Tree of comparisons made by a sorting algorithm. Each comparison reduces the number of possible orderings. Eventually, only one must remain. A decision tree is a full (not complete) binary tree; each node is a leaf or has degree 2.
11
Q. How many leaves does a decision tree have? A. There is one leaf for each permutation of n elements. There are n! permuatations. Q. What is the height of the tree? A. # of leaves = n! 2h Note the height is the worst-case number of comparisons that might be needed.
12
1. Use the item as an array index. 2. Examine the digits (or bits) of the item.
14
Counting Sort
Good for sorting integers in a narrow range Assume the input numbers (keys) are in the range 0..k Use an auxilliary array C[0..k] to hold the number of items less than i for 0 i k if k = O(n), then the running time is (n). Counting sort is stable; it keeps records in their original order.
15
16
17
Radix Sort
How IBM made its money, using punch card readers for census tabulation in early 1900s. Card sorters worked on one column at a time. Sort each digit (or field) separately. Start with the least-significant digit. Must use a stable sort.
RADIX-SORT(A, d) 1 for i 1 to d 2 do use a stable sort to sort array A on digit i
18
19