Chapter 6. HeapSort
Sorting Problem Input: A sequence of n numbers < a1, a2,..., an > Output: A permutation (reordering) of the input sequence such that ' ' ' < a a a > 1 2... n
HeapSort O(n lg n) worst case like merge sort Sorts in place like insertion sort Only a constant number of array elements are stored outside the input array at any time Combines the best of both algorithms Design technique: use of a data structure: heap To understand HeapSort, study Heaps and Heap operations
Heap: Data Structure(1) (Binary) Heap is a nearly complete binary tree. The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point Properties of a heap (represented by an array A) Length [A]: # of elements in A Heap-size [A]: # of elements in the heap stored within A Height of node = # of edges on a longest simple path from the node down to a leaf. Height of heap = height of root = Θ(lg n).
Heap: Data Structure(2) A heap can be stored as an array A[1.. n]. Root of tree is A[1]. Parent of A[i ] = A[ i / 2 ] Left child of A[i ] = A[2i ]. Right child of A[i ] = A[2i + 1]. Computing is fast with binary representation implementation.
Heap: Property For max-heaps (largest element at root), max-heap property: for all nodes i,, excluding the root, A[PARENT(i )] A[i]. For min-heaps (smallest element at root), min-heap property: for all nodes i,, excluding the root, A[PARENT(i )] A[i]. The maximum(or minimum) element of a max-heap(or min- heap) is at the root. The heap-sort algorithm uses max-heaps. Min-heap are commonly used in priority queues In general, heaps can be k-ary tree (instead of binary).
Maintaining the Heap property Always maintain the max-heap property: MAX-HEAPIFY Before MAX-HEAPIFY, A[i] may be smaller than its children. Assume left and right sub-trees of i are max-heaps. After MAX-HEAPIFY,, sub-tree rooted at i is a max-heap. The way MAX-HEAPIFY works: Compare A[i], A[LEFT(i)], and A[RIGHT(i)]. If necessary, swap A[i] with the larger of the two children to preserve heap property. Continue this process of comparing and swapping down the heap, until sub-tree rooted at i is max-heap. If we hit a leaf,, then the sub- tree rooted at the leaf is trivially a max-heap.
Sub-tree rooted at i
Time: O(h)=>O(lgn).
The children s subtree each have size at most 2n/3 T ( n) T (2 n / 3) + Θ(1) T ( n) = O(lg n)
Building a Heap Total time: O(n lg n)
Proof of Build-Heap
Bound analysis
Asymptotic Tight Bound
The HeapSort algorithm Given an input array, Builds a max-heap from the array. Starting with the root (the maximum element), the algorithm places the maximum element into the correct place in the array by swapping it with the element in the last position in the array. Discard this last node (knowing that it is in its correct place) by decreasing the heap size, and calling MAX-HEAPIFY on the new (possibly incorrectly-placed) root. Repeat this discarding process until only one node (the smallest element) remains, and therefore is in the correct place in the array.
Algorithm Sort in place Example:
1 10 9 2
Analysis: HEAPSORT(A, A, n) BUILD-MAX-HEAP( HEAP(A, A, n) : O(n) for i n down to 2 do : n-1 1 times exchange A[1] A[i] : O(1) MAX-HEAPFIY (A, 1,, i-1) : O(lg n) Total time: O(n lg n)
Priority Queue(1) A Priority Queue is a data structure for maintaining a dynamic set S of elements, each element has an associated value called a key. A priority queue is a data structure such that access or removal is of the highest-priority element in the collection, according to some method for comparing elements. Max-priority queue supports dynamic-set operations: INSERT(S, x): inserts element x into set S. MAXIMUM(S): returns element of S with largest key. EXTRACT-MAX(S): removes and returns element of S with largest key. INCREASE-KEY(S, x, k): increases value of element x s key to k.. Assume k x s current key value.
Priority Queue(2) Min-priority queue supports similar operations: INSERT(S, x): inserts element x into set S. MINIMUM(S): returns element of S with smallest key. EXTRACT-MIN(S): removes and returns element of S with smallest key. DECREASE-KEY(S, x, k): decreases value of element x s key to k.. Assume k x s current key value. Example of min-priority queue application: event-driven simulator. Example of max-priority queue application: job scheduling on a shared computer.
Heap implementation of Priority Queue Heaps efficiently implement priority queues. Max-heaps implemented with max-priority queues A Heap: a good compromise between fast insertion but slow extraction and vice versa. Both operations take O(lg n) time. Cf) A max-priority queues implemented by a Linked-List: insertion O(n) extraction O(1)
Finding the Maximum element: -- Get the root. MAXIMUM(S) Time: Θ(1)
Extracting Max element: EXTRACT-MAX(S) Given the array A: Make sure heap is not empty. Make a copy of the maximum element (the root). Make the last node in the tree the new root. Re-Heapify the heap, with one fewer node. Return the copy of the maximum element.
Analysis: Constant time assignment + Time for MAX-HEAPIFY Time: O(lg n).
Increasing Key Value: INCREASE-KEY(S, x, k) Given set S, element x, and new key value k: Make sure k x s current key. Update x s key value to k. Traverse the tree upward comparing x to its parent and swapping keys if necessary, until x s key is smaller than its parent s key.
Analysis: Upward path from node i has length O(lg n) in an n-element heap. Time: O(lg n)
Inserting into the Heap: INSERT(S, x) Given a key k to insert into the heap: Insert a new node in the very last position in the tree with key -. Increase the - key to k using the HEAP-INCREASE-KEY procedure defined above. Analysis: constant time assignments + time for HEAP-INCREASE-KEY. Time: O(lg n)
Homework 6.2-6, 6, 6.3-2,6.4-2,6.4-3 思考题 7-4 8