Download presentation
Presentation is loading. Please wait.
1
BuildHeap & HeapSort
2
Building a Heap Want to build a heap from array of N numbers
Make empty heap N times add number to heap
3
Building a Heap Want to build a heap from array of N numbers
Make empty heap : O(1) N times add number to heap : n * O(logn) Final BigO = O(nlogn)
4
Building a Heap Want to build a heap from array of N numbers
Make empty heap : O(1) N times add number to heap : n * O(logn) Make heap one at a time = O(nlogn)
5
Heapify Heapify : Move array values so they obey heap property
6
Heapify Build heap by heapifying existing array:
Work bottom up: swap each node with smallest child
7
Leaves Number value / 2 = last non leaf
9 nodes last non leaf is 9/2 = 4th 6 nodes last non leaf is 6/2 = 3rd
8
Heapify Build heap by heapifying existing array: Work bottom up
Get to skip leaves
9
Heapify Build heap by heapifying existing array: Work bottom up
Get to skip leaves
10
Heapify Build heap by heapifying existing array: Work bottom up
Get to skip leaves
11
Heapify Build heap by heapifying existing array: Work bottom up
Get to skip leaves
12
Heapify Build heap by heapifying existing array: Work bottom up
Get to skip leaves
13
Heapify Build heap by heapifying existing array: Work bottom up
Get to skip leaves
14
Heapify Build heap by heapifying existing array: Work bottom up
Get to skip leaves
15
Heapify Build heap by heapifying existing array:
May step down multiple times
16
Heapify Build heap by heapifying existing array:
May step down multiple times
17
Heapify Build heap by heapifying existing array:
May step down multiple times
18
Analysis Work depends on height of node Leaf = 0 Height 1 = 1
19
Analysis Work depends on height of node What is average work? Leaf = 0
20
Analysis Work depends on height of node What is average work?
Leaf = x 8 Height 1 = 1 x 4 Height 2 = 2 x 2 Height 3 = 3 x 1 What is average work? =12 𝑤𝑜𝑟𝑘 = .8 𝑎𝑣𝑔 𝑤𝑜𝑟𝑘
21
Analysis General Case Leaf ~ 0 x 1 2 Height 1 ~ 1 x 1 4
22
Analysis General Case Leaf ~ h x 1 2 ℎ+1 Height 1 ~ h x 1 2 ℎ+1
23
Analysis General Case Leaf ~ h x 1 2 ℎ+1 Height 1 ~ h x 1 2 ℎ+1
Average work per node = 1 Total work = O(n)
24
Build Heap Want to build a heap from list of N numbers
Throw numbers into array Heapify from N down to 0
25
Build Heap Want to build a heap from list of N numbers
Throw numbers into array : O(n) Heapify from N down to 0 : O(n) Final Big-O = O(n)
26
Application Problem: Remove k smallest items from an array Choices:
Remove one at a time k times Sort array ascending, chop off last k items Heapify array, do k remove min
27
Application Problem: Remove k smallest items from an array Choices:
Remove one at a time k times k * n Sort array ascending, chop off last k items Heapify array, do k remove min
28
Application Problem: Remove k smallest items from an array Choices:
Remove one at a time k times k * n Sort array ascending, chop off last k items nlogn Heapify array, do k remove min
29
Application Problem: Remove k smallest items from an array Choices:
Remove one at a time k times k * n Sort array ascending, chop off last k items nlogn + k Heapify array, do k remove min n + klogn
30
Application - Heapsort
Heapify values Repeat N - 1 times Swap root/last Decrement size Heapify root
31
HeapSort HeapSort : Put values into a heap Repeat N - 1 times
Swap root/last Decrement size Heapify root
32
HeapSort HeapSort : Put values into a heap Repeat N - 1 times
Swap root/last Decrement size Heapify root
33
HeapSort HeapSort : Put values into a heap Repeat N - 1 times
Swap root/last Decrement size Heapify root
34
HeapSort HeapSort : Put values into a heap Repeat N - 1 times
Swap root/last Decrement size Heapify root
35
HeapSort HeapSort : Put values into a heap Repeat N - 1 times
Swap root/last Decrement size Heapify root
36
HeapSort HeapSort : Put values into a heap Repeat N - 1 times
Swap root/last Decrement size Heapify root
37
HeapSort HeapSort : Put values into a heap : O(n) Repeat N - 1 times :
Swap root/last : O(1) Decrement size : O(1) Heapify root : O(logn)
38
HeapSort HeapSort : Overall = O(n + nlogn) = O(nlogn)
Put values into a heap : O(n) Repeat N - 1 times : O(nlogn) Swap root/last : O(1) Decrement size : O(1) Heapify root : O(logn) Overall = O(n + nlogn) = O(nlogn)
39
HeapSort So… Better than quicksort's worst case
For array : no extra memory (unlike mergesort)
40
HeapSort So… But… Better than quicksort's worst case
For array : no extra memory (unlike mergesort) But… Distant comparisons = poor use of cache
41
HeapSort So… But… Introsort Better than quicksort's worst case
For array : no extra memory (unlike mergesort) But… Distant comparisons = poor use of cache Introsort Quicksort, but switch to Heapsort once logN levels deep
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.