1 / 46

Lecture #5

Lecture #5. Heap Sort. Outline. This topic covers the simplest Q ( n lg ( n )) sorting algorithm: heapsort We will: define the strategy analyze the run time convert an unsorted list into a heap cover some examples. Heap.

katima
Download Presentation

Lecture #5

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture #5 Heap Sort

  2. Outline • This topic covers the simplest Q(nlg(n)) sorting algorithm: heapsort • We will: • define the strategy • analyze the run time • convert an unsorted list into a heap • cover some examples

  3. Heap • An array object that can be viewed as a nearly complete binary tree • 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

  4. Heap Sort • Strategy: given an unsorted list with n objects, place them into a heap, and take them out

  5. In-place Implementation • Instead of implementing a min-heap, consider a max-heap: • a heap where the maximum element is at the top of the heap and the next to be dequeued

  6. In-place Heapification • Consider the following unsorted array: • This array represents the following complete tree:

  7. In-place Heapification • This array, when interpreted as a complete tree, does not represent a max-heap • We must somehow convert the entries so that they represent a max-heap • Restriction: • the operation must be done in-place

  8. In-place Heapification • We must convert an unsorted list into a max-heap in-place • Consider the following unordered array together with the tree the array represents

  9. In-place Heapification • Clearly, this is not a max-heap, however all of the leaf nodes are (if considered by themselves) max-heaps • Also, the sub-tree with 87 as the root is a max-heap

  10. In-place Heapification • The sub-tree with 23 is not a heap, but swapping it with 55 creates a max-heap • This process is termed percolating down

  11. In-place Heapification • The sub-tree with 3 as the root is not max-heap, but we can swap 3 and 86

  12. In-place Heapification • Starting with the next higher level, the sub-tree with root 48 can be turned into a max-heap by swapping 48 and 99

  13. In-place Heapification • Similarly, swapping 61 and 95 creates a max-heap of the next sub-tree

  14. In-place Heapification • As does swapping 35 and 92

  15. In-place Heapification • The sub-tree with 24 as the root may be converted into a max-heap by first swapping 24 and 86 and then swapping 24 and 28

  16. In-place Heapification • The right-most sub-tree of the next higher level may be turned into a max-heap by swapping 77 and 99

  17. In-place Heapification • However, to turn the next sub-tree into a max-heap requires that 13 be percolated down to the deepest level

  18. In-place Heapification • The root need only be percolated down by two levels

  19. In-place Heapification • The final product is a max-heap

  20. Max-Heapify Algorithm • MAX-HEAPIFY is an important subroutine for manipulating max-heaps. Its inputs are an array A and an index i into the array. • When MAX-HEAPIFY is called, it is assumed that the binary trees rooted at LEFT(i ) and RIGHT(i ) are max-heaps, but that A[i ] may be smaller than its children, thus violating the max-heap property. • The function of MAX-HEAPIFY is to let the value at A[i ] “float down” in the maxheapso that the subtree rooted at index i becomes a max-heap.

  21. Max-Heapify Algorithm

  22. Run-time Analysis of Heapify • Considering only a perfect tree of height h, the maximum number of swaps which a second-lowest level would experience is 1, the next higher level, 2, and so on

  23. Run-time Analysis of Heapify • The running time of MAX-HEAPIFY on a subtree of size n rooted at given node iis the Q(1) time to fix up the relationships among the elements A[i], A[LEFT(i )], and A[RIGHT(i )], plus the time to run MAX-HEAPIFY on a subtreerooted at one of the children of node i. T(n) = Q(1)+T(??)

  24. Run-time Analysis of Heapify • 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

  25. Run-time Analysis of Heapify • The running time of MAX-HEAPIFY can therefore be described by the recurrence T (n) ≤ T (2n/3) + Q(1) • The solution to this recurrence, by case 2 of the master theorem, is T (n) = O(lg n). • Alternatively, we can characterize the running time of MAXHEAPIFY on a node of height h as O(h).

  26. BUILD-MAX-HEAP Algorithm • We can use the procedure MAX-HEAPIFY in a bottom-up manner to convert an array A[1 . . n], where n = length[A], into a max-heap. • The elements in the subarray A[(n/2+1) . . n] are all leaves of the tree, and so each is a 1-element heap to begin with. The procedure BUILD-MAX-HEAP goes through the remaining nodes of the tree and runs MAX-HEAPIFY on each one.

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

  28. Example Heap Sort • First we must convert the unordered array with n = 10 elements into a max-heap • None of the leaf nodes need tobe percolated down, and the firstnon-leaf node is in position n/2 • Thus we start with position 10/2 = 5

  29. Example Heap Sort • We compare 3 with its child and swap them

  30. Example Heap Sort • We compare 17 with its two children and swap it with the maximum child (70)

  31. Example Heap Sort • We compare 28 with its two children, 63 and 34, and swap it with the largest child

  32. Example Heap Sort • We compare 52 with its children, swap it with the largest • Recursing, no further swaps are needed

  33. Example Heap Sort • Finally, we swap the root with its largest child, and recurse, swapping 46 again with 81, and then again with 70

  34. Heap Sort Example • We have now converted the unsorted arrayinto a max-heap:

  35. Heap Sort 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(nlgn) • BUILD-MAX-HEAP: O(n) • Each of the n-1 calls to MAX-HEAPIFY takes time O(lgn)

  36. Heap Sort Algorithm

  37. Heap Sort Example • Suppose we dequeue the maximum element of this heap • This leaves a gap at the back of the array:

  38. Heap Sort Example • This is the last entry in the array, so why not fill it with the largest element? • Repeat this process: dequeue the maximum element, and then insert it at the end of the array:

  39. Heap Sort Example • Repeat this process • Dequeue and append 70 • Dequeueand append 63

  40. Heap Sort Example • We have the 4 largest elements in order • Dequeue and append 52 • Dequeueand append 46

  41. Heap Sort Example • Continuing... • Dequeue and append 34 • Dequeueand append 28

  42. Heap Sort Example • Finally, we can dequeue 17, insert it into the 2nd location, and the resulting array is sorted

  43. Heap Sort • Heapification runs in Q(n) • Dequeuingn items from a heap of size n, as we saw, runs in Q(nlg(n)) time • we are only making one additional copy into the blank left at the end of the array • Therefore, the total algorithm will run in Q(nlg(n)) time

  44. Heap Sort • There are no best-case and worst-case scenarios for heap sort • dequeuing from the heap will always require the same number of operations regardless of the distribution of values in the heap • The original order may speed up the heapification, however, this would only speed up an Q(n) portion of the algorithm

  45. Run-time Summary • The following table summarizes the run-times of heap sort

  46. Thanks

More Related