HeapSort
HeapSort
7 juin 2021
Outline
Introduction
Heaps
Operations on heaps
Heapify
Buildheap
Heapsort
New exercises
Exercises
Sorting
function Parent(i)
return bi/2c ;
function Left(i)
return 2 × i ;
function Right(i)
return 2 × i + 1 ;
The Heap Property
Heapify(A, i)
l = Left(i) ; r = Right(i) ;
if (l ≤ heap size(A) & A[l] > A[i])
largest = l ;
else
largest = i ;
if (r ≤ heap size(A) & A[r] > A[largest])
largest = r ;
if (largest != i)
Swap(A, i, largest) ;
Heapify(A, largest) ;
Example : heapify
This array A = [23, 11, 14, 9, 13, 10, 1, 5, 7, 12] is not a heap as
Parent(5) = b 2i c = 2, A[2] = 11 in the array, which violates the
max-heap property as A[5] = 13 is greater than A[2].
Left(5) = 10 and A[10] > A[5] which violates the heap property
A[Parent(i)] ≥ A[i]. Thus heapify continues, swap A[5] with
Left(5) = 2i = A[10]
T (n) ∈ Θ(log n)
Heap Operations : BuildHeap()
BuildHeap(A)
heap size(A) = length(A) ;
for (i = blength(A)/2c downto 1)
Heapify(A, i) ;
BuildHeap() Example
Run the algorithm BuildHeap(A) on the array A = [5, 3, 17, 10, 84, 19, 6, 22, 9]
Find i = b length(A)
2
c = b 29 c = 4, the entry in A where BuildHeap starts
blog nc blog nc
X n X h
d h+1
eO(h) = O(n )
2 2h
h=0 h=0
∞
X h
= O(n )
2h
h=0
= O(n)
Analyzing BuildHeap() : Tight
∞ ∞
X h X 1
= h( )h
2h 2
h=0 h=0
∞
X 1
= hx h where x =
2
h=0
∞
1/2 X 1
= 1 2
the closed form of h( )h
(1 − 2 ) 2
h=0
= 2
Heapsort
Heapsort(A)
BuildHeap(A) ;
for (i = length(A) downto 2)
Swap(A[1], A[i]) ;
heap size(A) = heap size(A) - 1 ;
Heapify(A, 1) ;
Analyzing Heapsort
= O(n) + (n − 1)O(log n)
= O(n) + O(n log n)
= O(n log n)
ExtractMax(A)
max = A[1]
A[1] = A[A.heap-size]
A.heap-size = A.heap-size -1
Heapify(A,1)
return max
Insert(A, key)
A.heap-size = A.heap-size + 1
A[A.heap-size] = key
i = A.heap-size
while i > 1 and A[Parent(i)] < A[i]
swap(A[i], A[Parent(i)])
i = Parent(i)
I Since the parent key is smaller than 10, the nodes are swapped
I Since the parent key is smaller than 10, the nodes are swapped
New exercises
1. Convert the array A = [10, 26, 52, 76, 13, 8, 3, 33, 60, 42] into a
maximum heap
2. Is this array [23, 17, 14, 6, 13, 10, 1, 5, 7, 12] a heap ? If not
make it a heap.
3. Run the algorithm BuildHeap(A) on the array
A = [12, 28, 36, 1, 37, 13, 4, 25, 3]. Show each step of your work
using the array representation of the modified heap
4. Heapsort A[12, 28, 4, 37]. Important, show each step of your work
using the array representation
5. Heapsort A = [25, 67, 56, 32, 12, 96, 82, 44] (very long)
New exercises continue
11. A d-ary heap is like a binary heap, but instead of 2 children, nodes
have d children.
11.1 Explain how would you represent a 3-ary heap in an array, i.e. give
the formulas for Parent(i), Left(i) and Right(i)
11.2 What is the height of a 3-ary heap of n elements ?
11.3 Sketch the idea of a heapify routine for a 3-ary heap
11.4 Give an implementation of ExtractMax() for a priority queue based
on a 3-ary heap
12. Show how to implement a regular FIFO queue using a
”min”-priority queue
13. Show how to implement a stack using a ”max”-priority queue
Exercises
1. Insertion sort and merge sort are stable algorithms while heapsort and
quicksort are not. Can you explain why this is so ?
2. Run Heapify (A, 3) on the array A = [27, 17, 3, 16, 13, 10, 1, 5, 7, 12, 4, 8, 9, 0]
Exercises
6. Run Heapsort(A) on the the array A = [3, 15, 2, 29, 6, 14, 25, 7, 5]
Exercises