This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: o can have duplicate key values k’s left child: 2k+1 k’s right child: 2k+2 k’s parent: (k-1)/2 [see notes on paper] Sift up-compare with parent o if > parent, switch & move up to next level, then repeat o else, quit Sift down-compare the two children, pick the largest one and swap with parent o while index is not out of bound, switch & move down a level, then repeat o else, quit Heap running time (worst case) • Insert - O(log n) o counting number of comparisons in sift up o if height of the heap is h number of comparisons is h what is h as a function of n? • delete - O(log n) o counting number of comparisons in sift down for running time o if height of heap is h number of comparisons is 2h Use heap for sorting -- heap sort 5 1 3 1 8 2 2 6 insert in a heap, one at a time 1 st delete -> 22 2 nd delete -> 18 running time • insert o O(log n!) = O(n log n) • delete o O(log n!) = O(n log n)...
View Full Document