Title: Heapsort
1Heapsort
2Heapsort
- Like MERGESORT, it runs in O(n lg n)
- Unlike MERGESORT, it sorts in place
- Based off of a heap, which has several uses
- The word heap doesnt refer to memory management
3The Heap
- A binary heap is a nearly complete binary tree
- Implemented as an array A
- Two similar attributes
- lengthA is the size (number of slots) in A
- heap-sizeA is the number of elements in A
- Thus, heap-sizeA ? lengthA
- Also, no element past Aheap-sizeA is an
element
4The Heap
- Can be a min-heap or a max-heap
87
51
67
55
43
41
25
87
21
17
33
35
87
51
67
25
41
55
43
21
17
33
35
5Simple Functions
- PARENT(i)
- return (i/2)
- LEFT(i)
- return (2i)
- RIGHT(i)
- return (2i 1)
6Properties
- Max-heap property
- APARENT(i) ? Ai
- Min-heap property
- APARENT(i) ? Ai
- Max-heaps are used for sorting
- Min-heaps are used for priority queues (later)
- We define the height of a node to be the longest
path from the node to a leaf. - The height of the tree is ?(lg n)
7MAX-HEAPIFY
- This is the heart of the algorithm
- Determines if an individual node is smaller than
its children - Parent swaps with largest child if that child is
larger - Calls itself recursively
- Runs in O(lg n) or O(h)
8HEAPIFY
- MAX-HEAPIFY (A, i)
- l ? LEFT (i)
- r ? RIGHT(i)
- if l heap-sizeA and Al gt Ai
- then largest ? l
- else largest ? i
- if r heap-sizeA and ArgtAlargest
- then largest ? r
- if largest ? i
- then exchange Ai with Alargest
- MAX-HEAPIFY (A, largest)
-
916
4
10
9
3
14
7
2
8
1
1016
14
10
9
3
4
7
2
8
1
1116
14
10
9
3
8
7
2
4
1
12Of Note
- The childrens subtrees each have size at most
2n/3 when the last row is exactly ½ full - Therefore, the running time is
- T (n) T(2n/3) ?(1) O(lg n)
13BUILD-HEAP
- Use MAX-HEAPIFY in bottom up manner
- Why does the loop start at lengthA/2?
- At the start of each loop, each node i is the
root of a max-heap! - BUILD-HEAP (A)
- heap-sizeA ? lengthA
- for i ? lengthA/2 downto 1
- do MAX-HEAPIFY(A, i)
14Analysis of Building a Heap
- Since each call to MAX-HEAPIFY costs O(lg n) and
there are O(n) calls, this is O(n lg n)... - Can derive a tighter bound do all nodes take
log n time? - Has at most n/2h1 nodes at any height (the more
the height, the less nodes there are) - It takes O(h) time to insert a node of height h.
15- Thus, the running time is 2n O(n)
The number of nodes at height h
Multiplied by their height
16HEAPSORT
- HEAPSORT (A)
- BUILD-HEAP(A)
- for i ? lengthA downto 2
- do exchange A1 with Ai
- heap-sizeA ? heap-sizeA - 1
- MAX-HEAPIFY(A, 1)
1716
14
10
9
3
8
7
2
4
1
181
14
10
9
3
8
7
2
4
16
Swap A1 ? Ai
1914
8
10
9
3
4
7
2
1
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
201
8
10
9
3
4
7
2
14
16
Swap A1 ? Ai
2110
8
9
1
3
4
7
2
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
222
8
9
1
3
4
7
10
14
16
Swap A1 ? Ai
239
8
3
1
2
4
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
242
8
3
1
9
4
7
10
14
16
Swap A1 ? Ai
258
7
3
1
9
4
2
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
261
7
3
8
9
4
2
10
14
16
Swap A1 ? Ai
277
4
3
8
9
1
2
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
282
4
3
8
9
1
7
10
14
16
Swap A1 ? Ai
294
2
3
8
9
1
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
301
2
3
8
9
4
7
10
14
16
Swap A1 ? Ai
313
2
1
8
9
4
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
321
2
3
8
9
4
7
10
14
16
Swap A1 ? Ai
332
1
3
8
9
4
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
341
2
3
8
9
4
7
10
14
16
Swap A1 ? Ai
351
2
3
8
9
4
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
36Priority Queues
- A priority queue is a heap that uses a key
- Common in operating systems (processes)
- Supports HEAP-MAXIMUM, EXTRACT-MAX, INCREASE-KEY,
INSERT - HEAP-MAXIMUM (A)
- 1 return A1
37- HEAP-EXTRACT-MAX (A)
- 1 if heap-sizeA lt 1
- 2 then error heap underflow
- 3 max ? A1
- 4 A1 ? Aheap-sizeA
- 5 heap-sizeA ? heap-sizeA 1
- 6 MAX-HEAPIFY (A, 1)
- 7 return max
38- HEAP-INCREASE-KEY(A, i, key)
- 1 if key lt Ai
- 2 then error new key smaller than current
- 3 Ai ? key
- 4 while i gt 1 and APARENT(i) lt Ai
- 5 do exchange Ai ? APARENT(i)
- 6 i ? PARENT(i)
Note runs in O(lg n)
39- MAX-HEAP-INSERT (A, key)
- 1 heap-sizeA ? heap-sizeA 1
- 2 Aheap-sizeA ? -?
- 3 HEAP-INCREASE-KEY(A, heap-sizeA, key)