Heapsort

1 / 37

# Heapsort - PowerPoint PPT Presentation

Heapsort. Introduction. Heapsort Running time: O( n lg n ) Like merge sort Sort in place: only a constant number of array elements are stored outside the input array at any time Like insertion sort Heap

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## Heapsort

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
1. Heapsort

2. Introduction • Heapsort • Running time: O(n lg n) • Like merge sort • Sort in place: only a constant number of array elements are stored outside the input array at any time • Like insertion sort • Heap • A data structure used by Heapsort to manage information during the execution of the algorithm • Can be used as an efficient priority queue

3. Heaps

4. Binary Heap • An array object that can be viewed as a nearly complete binary tree (see Section B.5.3) • Each tree node corresponds to an array element that stores the value in the tree node • The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point • A has two attributes • length[A]: # of elements in the array • heap-size[A]: # of elements in the heap stored within A • heap-size[A]  length[A] • No element past A[heap-size[A]] is an element of the heap • max-heap and min-heap

5. n-2 n-1 n Balanced Balanced Not balanced Balanced binary trees • Recall: • The depth of a node is its distance from the root • The depth of a tree is the depth of the deepest node • A binary tree of depth n is balanced if all the nodes at depths 0throughn-2 have two children

6. A Max-Heap

7. Length and Heap-Size 11 7 11 7 Length = 10 Heap-Size = 7

8. Heap Computation • Given the index i of a node, the indices of its parent, left child, and right child can be computed simply:

9. Heap Property • Heap property – the property that the values in the node must satisfy • Max-heap property: for every node i other than the root • A[PARENT(i)]  A[i] • The value of a node is at most the value of its parent • The largest element in a max-heap is stored at the root • The subtree rooted at a node contains values on larger than that contained at the node itself • Min-heap property: for every node i other than the root • A[PARENT(i)]  A[i]

10. A node has the heap property if the value in the node is as large as or larger than the values in its children All leaf nodes automatically have the heap property A binary tree is a heap if all nodes in it have the heap property 12 12 12 8 3 8 12 8 14 Blue node has heap property Blue node has heap property Blue node does not have heap property The heap property

11. Heap Height • The height of a node in a heap is the number of edges on the longest simple downward path from the node to a leaf • The height of a heap is the height of its root • The height of a heap of n elements is (lg n)

12. Heap Procedures • MAX-HEAPIFY : maintain the max-heap property • O(lg n) • BUILD-MAX-HEAP: produces a max-heap from an unordered input array • O(n) • HEAPSORT: sorts an array in place • O(n lg n) • MAX-HEAP-INSERT, HEAP-EXTRACT, HEAP-INCREASE-KEY, HEAP-MAXIMUM: allow the heap data structure to be used as a priority queue • O(lg n)

13. Maintaining the Heap Property • MAX-HEAPIFY • Inputs: an array A and an index i into the array • Assume the binary tree rooted at LEFT(i) and RIGHT(i) are max-heaps, but A[i] may be smaller than its children (violate the max-heap property) • MAX-HEAPIFY let the value at A[i] floats down in the max-heap

14. Given a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the larger child Notice that the child may have lost the heap property 14 12 8 12 8 14 Blue node has heap property Blue node does not have heap property MAX-HEAPIFY

15. Example of MAX-HEAPIFY

16. MAX-HEAPIFY Extract the indices of LEFT and RIGHT children of i Choose the largest of A[i], A[l], A[r] Float down A[i] recursively

17. Running time of MAX-HEAPIFY • (1) to find out the largest among A[i], A[LEFT(i)], and A[RIGHT(i)] • Plus the time to run MAX-HEAPIFY on a subtree rooted at one of the children of node i • The children’s subtrees each have size at most 2n/3 – the worst case occurs when the last row of the tree is exactly half full • T(n)  T(2n/3) + (1) • By case 2 of the master theorem: T(n) = O(lg n) 7/11

18. Building A Heap

19. Build Max Heap • Observation: A[(n/2+1)..n] are all leaves of the tree • Each is a 1-element heap to begin with • Upper bound on the running time • O(lg n) for each call to MAX-HEAPIFY, and call n times  O(n lg n) • Not tight

20. The HeapSort Algorithm

21. Idea • Using BUILD-MAX-HEAP to build a max-heap on the input array A[1..n], where n=length[A] • Put the maximum element, A[1], to A[n] • Then discard node n from the heap by decrementing heap-size(A) • A[2..n-1] remain max-heaps, but A[1] may violate • call MAX-HEAPIFY(A, 1) to restore the max-heap property for A[1..n-1] • Repeat the above process from n down to 2 • Cost: O(n lg n) • BUILD-MAX-HEAP: O(n) • Each of the n-1 calls to MAX-HEAPIFY takes time O(lg n)

22. Next Slide Example of HeapSort

23. Example of HeapSort (Cont.) 1 10 1 14 14 10 9 1 14

24. Algorithm

25. Priority Queues

26. Definition • A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key. A max-priority queue supports the following operations: • INSERT(S, x) inserts the element x into the set S • MAXIMUM(S) returns the element of S with the largest key • EXTRACT-MAX(S) removes and returns the element of S with the largest key • INCREASE-KEY(S, x, k) increases the value of element x’s key to the new value k, which is assumed to be at least as largest as x’s current key value • Application of max-priority queue: Job scheduling in computer

27. HEAP-MAXIMUM and HEAP-EXTRACT-MAX (1) O(lg n)

28. Here’s a sample binary tree after it has been heapified Notice that heapified does not mean sorted Heapifying does not change the shape of the binary tree; this binary tree is balanced and left-justified because it started out that way 25 22 17 19 22 14 15 18 14 21 3 9 11 A sample heap

29. Notice that the largest number is now in the root Suppose we discard the root: Solution: remove the rightmost leaf at the deepest level and use it for the new root 11 22 17 19 22 14 15 18 14 21 3 9 11 Removing the max node

30. However, only the root lacks the heap property We can MAX_HEAPIFY(A,1) the root After doing this, one and only one of its children may have lost the heap property 11 22 17 19 22 14 15 18 14 21 3 9

31. Now the left child of the root (still the number 11) lacks the heap property We can MAX_HEAPIFY(A,2) this node After doing this, one and only one of its children may have lost the heap property 22 11 17 19 22 14 15 18 14 21 3 9

32. Now the right child of the left child of the root (still the number 11) lacks the heap property: We can MAX_HEAPIFY(A,5) this node After doing this, one and only one of its children may have lost the heap property —but it doesn’t, because it’s a leaf 22 22 17 19 11 14 15 18 14 21 3 9

33. Our tree is once again a heap, because every node in it has the heap property Once again, the largest (or a largest) value is in the root This produces a sequence of values in order largest to smallest 22 22 17 19 21 14 15 18 14 11 3 9

34. HEAP-INCREASE-KEY • Steps • Update the key of A[i] to its new value • May violate the max-heap property • Traverse a path from A[i] toward the root to find a proper place for the newly increased key O(lg n)

35. Example of HEAP-INCREASE-KEY

36. MAX-HEAP-INSERT O(lg n)