If you needed to hire someone to develop a scheduling application,
which team would you hire?
If you had to develop a scheduling application, which codebase
would you want to start with?
Kitschupit (YK et al.)
Reliability, Inc. (WR et al.)
We Have Not Discussed Our Team Name (PS et al.)
No (CM and company)
LEAH is Pretty Rad (LG et al.)
Email me your presentations (4 slides, including title) by 11 p.m.
on Tuesday evening.
Core: Explain the algorithm you've designed.
Sales: Distinguish your work from that of your peers.
(You may also want to distribute the schedule you've created.)
Students who might be called upon to generalize your code or
write a UI for your code.
The director of the midwest conference (I hope).
Priority queues, revisited
A linear structure in which elements are removed in a particular
order - highest priority first.
Linear structures have two operations: Put and get.
Implementation strategy one: Store the elements in sorted order
(in a linked list or cyclic array).
put might seem to be O(logn) because we can do binary search to find
the location, but ...
If we use linked lists, we can't do binary search, so O(n)
If we use arrays, we have to shift elements, so O(n)
With this implementation, get might be O(n) because things with linked
lists always seem to be O(n). But the largest is the first element,
so we can just get and drop it in O(1). If it's a cyclic array, it's
Can we do better?
Recent implementation techniques
Arrange arrays cleverly (e.g., in our design of hash tables)
Use linked structures cleverly (e.g., using trees)
Use randomness (e.g., in skip lists)
A heap is a tree that has two characteristics
"Nearly complete" - Every level must be full, except the last,
which may be full. If the last level is not full,
elements in the last level are all packed at the left.
"Heap property" - The value in a node is higher priority
(greater than or equal to) the values in nodes below it.
They are trees, but not binary search trees
Searching will be slow
Printing all the elements in order will be complex
Adding elements to heaps
Nearly complete is the harder property to maintain, so put the new
element in a position that maintains nearly complete.
We need to restore the heap property.
We repeatedly swap the element with its parent until we reach a parent
that has higher priority
Removing elements from heaps
Grab the top element to prepare to return it
Take the last element from the last level and put it at the top
Swap down - Repeatedly swap with the higher of the children, provided
you are smaller than that child
Adding an element is O(logn)
Removing an element is O(logn)
You have traverse (down to the bottom or up from the top)
Insert all the elements into a heap: O(nlogn)
Remove all the elements from the heap O(nlogn)
Storing heaps in arrays
Store them breadth first, left-to-right
Heap sort, revisited
Build the heap in place
Repeatedly swap the largest element to the end in place
We have a guaranteed O(nlogn) algorithm that uses a constant amount
of extra space.
I don't know why people use Quicksort instead of Heapsort