Feb 12
Start chapter 4 material: sorting.
Why?
- extremely common task
- building block for many other problems
- many techniques, illustrating a variety of algorithm design ideas
- fun and profit !
heaps & heapsort
Very cool datastructure. Common implementation of "priority queue"
- binary tree
- ... but typically stored as sequential array
- not designed for binary search
- ... but instead each node is max (or min) of its sub-nodes
- API
- find_min i.e. peek : O(1)
- pop_min : O(log n)
- insert : O(log n)
- build_heap from n things : O(n)
There are a number of variations.
heapsort : given N things, put 'em in a heap, then pop_min repeatedly. Gives O(n log(n)).
The text describes this as a "datastructure" approach to finding the algorithm. Really ... it's a classic cute trick; I'm not sure how
this one generalizes to finding algorithms for other problems.
merge sort
The general approach here is called "divide-and-conquer",
and is applicable to many problems. Typically gives a log2(n)
(rather than n) improvement.
Another O(n log(n))
Disadvantage: may need extra space.
quicksort
Another divide-and-conquer, but using "pivots",
often chosen "randomly".
- Average performance is O(n log(n)), though worst is O(n**2).
- Can be done in place, without extra memory.
- May not be "stable" (i.e. equal keys keep original order).
- Many variations, depending on how pivot is chosen and partitioning is done.
This is in practice one of the fastest sorts
(for reasons that aren't obvious) and is
a common choice if little is known in advance
about the things to sort.
In class: