460 likes | 576 Views
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.
E N D
Lecture #5 Heap Sort
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
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
Heap Sort • Strategy: given an unsorted list with n objects, place them into a heap, and take them out
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
In-place Heapification • Consider the following unsorted array: • This array represents the following complete tree:
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
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
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
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
In-place Heapification • The sub-tree with 3 as the root is not max-heap, but we can swap 3 and 86
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
In-place Heapification • Similarly, swapping 61 and 95 creates a max-heap of the next sub-tree
In-place Heapification • As does swapping 35 and 92
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
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
In-place Heapification • However, to turn the next sub-tree into a max-heap requires that 13 be percolated down to the deepest level
In-place Heapification • The root need only be percolated down by two levels
In-place Heapification • The final product is a max-heap
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.
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
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(??)
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
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).
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.
Remember : • Given the index i of a node, the indices of its parent, left child, and right child can be computed simply:
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
Example Heap Sort • We compare 3 with its child and swap them
Example Heap Sort • We compare 17 with its two children and swap it with the maximum child (70)
Example Heap Sort • We compare 28 with its two children, 63 and 34, and swap it with the largest child
Example Heap Sort • We compare 52 with its children, swap it with the largest • Recursing, no further swaps are needed
Example Heap Sort • Finally, we swap the root with its largest child, and recurse, swapping 46 again with 81, and then again with 70
Heap Sort Example • We have now converted the unsorted arrayinto a max-heap:
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)
Heap Sort Example • Suppose we dequeue the maximum element of this heap • This leaves a gap at the back of the array:
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:
Heap Sort Example • Repeat this process • Dequeue and append 70 • Dequeueand append 63
Heap Sort Example • We have the 4 largest elements in order • Dequeue and append 52 • Dequeueand append 46
Heap Sort Example • Continuing... • Dequeue and append 34 • Dequeueand append 28
Heap Sort Example • Finally, we can dequeue 17, insert it into the 2nd location, and the resulting array is sorted
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
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
Run-time Summary • The following table summarizes the run-times of heap sort