Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 261 – Data Structures BuildHeap and Heap Sort.

Similar presentations


Presentation on theme: "CS 261 – Data Structures BuildHeap and Heap Sort."— Presentation transcript:

1 CS 261 – Data Structures BuildHeap and Heap Sort

2 Heap Implementation: Constructors void buildHeap(struct dyArray * data) { int I; int max = dyArraySize(data); // All nodes greater than max/2 - 1 are leaves and thus adhere to the heap property! for (i = max / 2 - 1; i >= 0; i--) adjustHeap(data, max, i); // Make subtree rooted at i a heap. } At the beginning, only the leaves are proper heaps: –Leaves are all nodes with indices greater than max / 2 At each step, the subtree rooted at index i becomes a heap

3 Heap Implementation: Build heap void buildHeap(struct vecctor * data) { int I; int max = vectorSize(data); // All nodes greater than max/2 - 1 are leaves and thus adhere to the heap property! for (i = max / 2 - 1; i >= 0; i--) adjustHeap(data, max, i); // Make subtree rooted at i a heap. } For all subtrees that are are not already heaps (initially, all inner, or non-leaf, nodes) : –Call adjustHeap with the largest node index that is not already guaranteed to be a heap –Iterate until the root node becomes a heap Why call adjustHeap with the largest non-heap node? –Because its children, having larger indices, are already guaranteed to be heaps

4 Heap Implementation: adjustHeap 12 5 4 7 783 911102 1717 2525 3838 4343 0 12 11 max 5757 6464 7979 8 11 9 10 2 i ( max/2-1 ) Already heaps (leaf nodes) adjustHeap First iteration: adjust largest non-leaf node (index 4)

5 Heap Implementation: adjustHeap (cont.) 12 5 4 7 782 911103 1717 2525 3838 4242 0 12 11 max 5757 6464 7979 8 11 9 10 3 i (no adjustment needed) Already heaps adjustHeap Second iteration: adjust largest non-heap node (index 3)

6 Heap Implementation: adjustHeap (cont.) 12 5 4 7 782 911103 1717 2525 3838 4242 0 12 11 max 5757 6464 7979 8 11 9 10 3 i Already heaps adjustHeap Third iteration: adjust largest non-heap node (index 2)

7 Heap Implementation: adjustHeap (cont.) 12 4 5 7 782 911103 1717 2424 3838 4242 0 12 11 max 5757 6565 7979 8 11 9 10 3 i Already heaps adjustHeap Fourth iteration: adjust largest non-heap node (index 1)

8 Heap Implementation: adjustHeap (cont.) 12 4 5 2 783 911107 1212 2424 3838 4343 0 12 11 max 5757 6565 7979 8 11 9 10 7 i Already heaps Fifth iteration: adjust largest non-heap node (index 0  root) adjustHeap

9 Heap Implementation: adjustHeap (cont.) 2 4 5 3 787 9111012 1313 2424 3838 4747 0202 5757 6565 7979 8 11 9 10 12 Already heaps (entire tree)

10 Heap Sort - Basic idea Build the initial heap Repeately –Remove the smallest element (root) –Rebuild the heap So the heap starts out with n elements, then has n-1, then n-2, and so on Where should you store the elements that are removed? Why not put them in the other end of the array? (The one that is no longer being used for the heap).

11 Heap Implementation: sort void heapSort(struct dyArray * data) { int i; buildHeap(data); // Build initial heap. for (i = dyArraySize(data)–1; i > 0; i--) { // For each of the n elements: dyArraySwap(data,0, i); // Swap last element with the first (smellest) element adjustHeap(data, i, 0); // Rebuild heap property. } Sorts the data in descending order (from largest to smallest) : –Builds heap from initial (unsorted) data –Iteratively swaps the smallest element (at index 0) with last unsorted element –Adjust the heap after each swap, but only considers the unsorted data

12 View from Middle of Execution

13 Heap Analysis: sort Execution time: –Build heap: n calls to adjustHeap = n log n –Loop: n calls to adjustHeap = n log n –Total: 2n log n = O(n log n) Advantages/disadvantages: –Same average as merge sort and quick sort –Doesn’t require extra space as the merge sort does –Doesn’t suffer if data is already sorted or mostly sorted

14 On the worksheet I’m having you represent the heap as a tree (easier to visualize than the array representation) Build the initial heap Then repeately remove the smallest element, then build the heap again, until there are no elements in the tree.


Download ppt "CS 261 – Data Structures BuildHeap and Heap Sort."

Similar presentations


Ads by Google