Sorting Part 3 Lectures by Suman Banerjee
Sorting Part 3 Lectures by Suman Banerjee
The Heap
21-Apr-17 2
The Heap
Can be a min-heap or a max-heap
87
51 67
25 41 55 43
87
21 17 33 35
87 51 67 25 41 55 43 21 17 33 35
21-Apr-17 3
Simple Functions
PARENT(i)
return (i/2)
LEFT(i)
return (2i)
RIGHT(i)
return (2i + 1)
21-Apr-17 4
Properties
Max-heap property:
A[PARENT(i)] A[i]
Min-heap property:
A[PARENT(i)] A[i]
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)
21-Apr-17 5
MAX-HEAPIFY
21-Apr-17 6
HEAPIFY
MAX-HEAPIFY (A, i)
l ← LEFT (i)
r ← RIGHT(i)
if l ≤ heap-size[A] and A[l] > A[i]
then largest ← l
else largest ← i
if r ≤ heap-size[A] and A[r]>A[largest]
then largest ← r
if largest ≠ i
then exchange A[i] with A[largest]
MAX-HEAPIFY (A, largest)
21-Apr-17 7
16
4 10
14 7 9 3
2 8 1
21-Apr-17 8
16
14 10
4 7 9 3
2 8 1
21-Apr-17 9
16
14 10
8 7 9 3
2 4 1
21-Apr-17 10
BUILD-HEAP
Use MAX-HEAPIFY in bottom up manner
Why does the loop start at length[A]/2?
At the start of each loop, each node i is the
root of a max-heap!
BUILD-HEAP (A)
heap-size[A] ← length[A]
for i ← length[A]/2 downto 1
do MAX-HEAPIFY(A, i)
21-Apr-17 11
HEAPSORT
HEAPSORT (A)
BUILD-HEAP(A)
for i ← length[A] downto 2
do exchange A[1] with A[i]
heap-size[A] ← heap-size[A] - 1
MAX-HEAPIFY(A, 1)
21-Apr-17 12
16
14 10
8 7 9 3
2 4 1
21-Apr-17 13
1
14 10
8 7 9 3
2 4 16
8 10
4 7 9 3
2 1 16
heap-size heap-size – 1
21-Apr-17 15
MAX-HEAPIFY(A, 1)
1
8 10
4 7 9 3
2 14 16
8 9
4 7 1 3
2 14 16
heap-size heap-size – 1
21-Apr-17 17
MAX-HEAPIFY(A, 1)
2
8 9
4 7 1 3
10 14 16
8 3
4 7 1 2
10 14 16
heap-size heap-size – 1
21-Apr-17 19
MAX-HEAPIFY(A, 1)
2
8 3
4 7 1 9
10 14 16
7 3
4 2 1 9
10 14 16
heap-size heap-size – 1
21-Apr-17 21
MAX-HEAPIFY(A, 1)
1
7 3
4 2 8 9
10 14 16
4 3
1 2 8 9
10 14 16
heap-size heap-size – 1
21-Apr-17 23
MAX-HEAPIFY(A, 1)
2
4 3
1 7 8 9
10 14 16
2 3
1 7 8 9
10 14 16
heap-size heap-size – 1
21-Apr-17 25
MAX-HEAPIFY(A, 1)
1
2 3
4 7 8 9
10 14 16
2 1
4 7 8 9
10 14 16
heap-size heap-size – 1
21-Apr-17 27
MAX-HEAPIFY(A, 1)
1
2 3
4 7 8 9
10 14 16
1 3
4 7 8 9
10 14 16
heap-size heap-size – 1
21-Apr-17 29
MAX-HEAPIFY(A, 1)
1
2 3
4 7 8 9
10 14 16
2 3
4 7 8 9
10 14 16
heap-size heap-size – 1
21-Apr-17 31
MAX-HEAPIFY(A, 1)
C/C++ Code
void MAX_HEAPIFY (int A[], int i) {
int l = LEFT(i);
int r = RIGHT(i);
int largest;
if (largest != i) {
exchange (&A[i], &A[largest]);
MAX_HEAPIFY (A, largest);
}
} 21-Apr-17 32
C/C++ Code
void BUILD_HEAP (int A []) {
heapsize = length;
for (int i = length/2;i >= 1; --i)
MAX_HEAPIFY(A, i);
}
21-Apr-17 35
Bucket Sort
Given that, we have N integers in the range 0 to M-
1
Maintain an array count of size M, which is
initialized to zero. Thus, count has M cells
(buckets).
Read Ai
Increment count[Ai] by 1
After all the input array is read, scan the count
array, printing out a representation of sorted list.
21-Apr-17 36
The End
21-Apr-17 37