BuildHeap & HeapSort
Building a Heap Want to build a heap from array of N numbers Make empty heap N times add number to heap
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)
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)
Heapify Heapify : Move array values so they obey heap property
Heapify Build heap by heapifying existing array: Work bottom up: swap each node with smallest child
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
Heapify Build heap by heapifying existing array: Work bottom up Get to skip leaves
Heapify Build heap by heapifying existing array: Work bottom up Get to skip leaves
Heapify Build heap by heapifying existing array: Work bottom up Get to skip leaves
Heapify Build heap by heapifying existing array: Work bottom up Get to skip leaves
Heapify Build heap by heapifying existing array: Work bottom up Get to skip leaves
Heapify Build heap by heapifying existing array: Work bottom up Get to skip leaves
Heapify Build heap by heapifying existing array: Work bottom up Get to skip leaves
Heapify Build heap by heapifying existing array: May step down multiple times
Heapify Build heap by heapifying existing array: May step down multiple times
Heapify Build heap by heapifying existing array: May step down multiple times
Analysis Work depends on height of node Leaf = 0 Height 1 = 1
Analysis Work depends on height of node What is average work? Leaf = 0
Analysis Work depends on height of node What is average work? Leaf = 0 x 8 Height 1 = 1 x 4 Height 2 = 2 x 2 Height 3 = 3 x 1 What is average work? 8 0 +4 1 +2 2 +3 1 =12 𝑤𝑜𝑟𝑘 12 15 = .8 𝑎𝑣𝑔 𝑤𝑜𝑟𝑘
Analysis General Case Leaf ~ 0 x 1 2 Height 1 ~ 1 x 1 4
Analysis General Case Leaf ~ h x 1 2 ℎ+1 Height 1 ~ h x 1 2 ℎ+1
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)
Build Heap Want to build a heap from list of N numbers Throw numbers into array Heapify from N down to 0
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)
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
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
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
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
Application - Heapsort Heapify values Repeat N - 1 times Swap root/last Decrement size Heapify root
HeapSort HeapSort : Put values into a heap Repeat N - 1 times Swap root/last Decrement size Heapify root
HeapSort HeapSort : Put values into a heap Repeat N - 1 times Swap root/last Decrement size Heapify root
HeapSort HeapSort : Put values into a heap Repeat N - 1 times Swap root/last Decrement size Heapify root
HeapSort HeapSort : Put values into a heap Repeat N - 1 times Swap root/last Decrement size Heapify root
HeapSort HeapSort : Put values into a heap Repeat N - 1 times Swap root/last Decrement size Heapify root
HeapSort HeapSort : Put values into a heap Repeat N - 1 times Swap root/last Decrement size Heapify root
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)
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)
HeapSort So… Better than quicksort's worst case For array : no extra memory (unlike mergesort)
HeapSort So… But… Better than quicksort's worst case For array : no extra memory (unlike mergesort) But… Distant comparisons = poor use of cache
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