|
|
|
Zasha Weinberg in lieu of Steve Wolfman |
|
Winter Quarter 2000 |
|
|
|
|
|
Simple: Selection Sort |
|
(some say Insertion Sort, or Bubble Sort) |
|
Quick: QuickSort |
|
Good worst case: MergeSort, HeapSort |
|
|
|
Can we do better? |
|
|
|
|
Find the smallest element, put it first |
|
Find the next smallest element, put it second |
|
Find the next smallest, put it next |
|
etc. |
|
|
|
|
|
|
|
|
|
|
|
Randomly permute input |
|
Bad cases more common than simple probability
would suggest. So, make it truly
random. |
|
Pick pivot cleverly |
|
“Median-of-3” rule takes Median(first, middle,
last element elements). |
|
Use MergeSort or HeapSort. |
|
|
|
|
Model expected # of operations |
|
Manipulate the recurrences |
|
Get O(n log n) |
|
(Weiss, p. 275-279) |
|
|
|
|
|
|
|
For any possible correct Sorting by Comparison
alg |
|
What is lowest best case time? |
|
What is lowest worst case time? |
|
|
|
|
|
How many comparisons does it take before we can
be sure of the order? |
|
This is the minimum # of comparisons that any
algorithm could do. |
|
|
|
|
|
How many leaves does the tree have? |
|
What’s the shallowest tree with a given number
of leaves? |
|
|
|