Presentation is loading. Please wait.

Presentation is loading. Please wait.

Build Heap and Heap Sort

Similar presentations


Presentation on theme: "Build Heap and Heap Sort"— Presentation transcript:

1 Build Heap and Heap Sort
CS 261 – Data Structures Build Heap and Heap Sort

2 Building a Heap At the beginning, only the leaves are proper heaps:
void buildHeap(struct DynArr *arr) { int max = sizeDynArr(arr); /* All nodes greater than max/2 - 1 are leaves and thus adhere to the heap property! */ for (int i = max / 2 - 1; i >= 0; i--) _adjustHeap(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 DynArr *arr) { int max = sizeDynArr(arr); /* All nodes greater than max/2 - 1 are leaves and thus adhere to the heap property! */ for (int i = max / 2 - 1; i >= 0; i--) _adjustHeap(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: Build heap (cont.)
12 First iteration: adjust largest non-leaf node (index 4) 7 5 adjustHeap 8 3 7 4 i (max/2-1) 9 11 10 2 max 12 1 7 2 5 3 8 4 3 5 7 6 4 7 9 8 11 9 10 10 2 11 Already heaps (leaf nodes)

5 Heap Implementation: Build heap (cont.)
12 Second iteration: adjust largest non-heap node (index 3) 7 5 adjustHeap 8 2 7 4 9 11 10 3 i (no adjustment needed) max 12 1 7 2 5 3 8 4 2 5 7 6 4 7 9 8 11 9 10 10 3 11 Already heaps

6 Heap Implementation: Build heap (cont.)
12 Third iteration: adjust largest non-heap node (index 2) 7 5 adjustHeap 8 2 7 4 9 11 10 3 i max 12 1 7 2 5 3 8 4 2 5 7 6 4 7 9 8 11 9 10 10 3 11 Already heaps

7 Heap Implementation: Build heap (cont.)
12 Fourth iteration: adjust largest non-heap node (index 1) adjustHeap 7 4 8 2 7 5 9 11 10 3 max 12 1 7 2 4 3 8 4 2 5 7 6 5 7 9 8 11 9 10 10 3 11 Already heaps i

8 Heap Implementation: Build heap (cont.)
12 Fifth iteration: adjust largest non-heap node (index 0  root) adjustHeap 2 4 8 3 7 5 9 11 10 7 max 12 1 2 2 4 3 8 4 3 5 7 6 5 7 9 8 11 9 10 10 7 11 Already heaps i

9 Heap Implementation: Build heap (cont.)
2 3 4 8 7 7 5 9 11 10 12 2 1 3 2 4 3 8 4 7 5 7 6 5 7 9 8 11 9 10 10 12 Already heaps (entire tree)

10 Heap Sort: Implementation
void heapSort(struct DynArr *arr) { /* Sorts data in reverse order. */ int i = sizeDynArr(arr); buildHeap(arr); /* Build initial heap. */ while (--i > 0) { /* For each of the n elements: */ swap(arr, 0, i); /* Swap first (smallest) and last (ith) elements. */ _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

11 Heap Sort: View During Execution

12 Heap Sort: Algorithmic Complexity
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

13 Your Turn Lesson 23: Build Heaps and Heap Sort


Download ppt "Build Heap and Heap Sort"

Similar presentations


Ads by Google