0% found this document useful (0 votes)
5 views37 pages

Sorting Part 3 Lectures by Suman Banerjee

Heapsort is a sorting algorithm that utilizes a binary heap data structure, which can be a min-heap or max-heap. The algorithm involves building a max-heap from the input data and then repeatedly extracting the maximum element to sort the array. The time complexity of Heapsort is O(n log n) for best, worst, and average cases.

Uploaded by

Tamajit Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views37 pages

Sorting Part 3 Lectures by Suman Banerjee

Heapsort is a sorting algorithm that utilizes a binary heap data structure, which can be a min-heap or max-heap. The algorithm involves building a max-heap from the input data and then repeatedly extracting the maximum element to sort the array. The time complexity of Heapsort is O(n log n) for best, worst, and average cases.

Uploaded by

Tamajit Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Heapsort

The Heap

A binary heap is a nearly complete binary tree


Implemented as an array A
Two similar attributes:
length[A] is the size (number of slots) in A
heap-size[A] is the number of elements in A
Thus, heap-size[A]  length[A]
Also, no element past A[heap-size[A]] is an
element

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

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)

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

Swap A[1]  A[i]


21-Apr-17 14
14

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

Swap A[1]  A[i]


21-Apr-17 16
10

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

Swap A[1]  A[i]


21-Apr-17 18
9

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

Swap A[1]  A[i]


21-Apr-17 20
8

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

Swap A[1]  A[i]


21-Apr-17 22
7

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

Swap A[1]  A[i]


21-Apr-17 24
4

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

Swap A[1]  A[i]


21-Apr-17 26
3

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

Swap A[1]  A[i]


21-Apr-17 28
2

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

Swap A[1]  A[i]


21-Apr-17 30
1

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 (l <= heapsize && A[l] > A[i])


largest = l;
else
largest = i;

if (r <= heapsize && A[r]>A[largest])


largest = r;

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);
}

void HEAPSORT (int A[]) {


BUILD_HEAP(A);

for (int i = length;i >= 2; --i) {


exchange (&A[1], &A[i]);
heapsize = heapsize - 1;
MAX_HEAPIFY(A, 1);
}
}
21-Apr-17 33
C/C++Code
int length = 5;
int heapsize = 0;
#define LEFT(i) 2*i
#define RIGHT(i) (2*i + 1)
void exchange (int *number1, int *number2) {
int temp;
temp = *number1;
*number1 = *number2;
*number2 = temp;
}
void main () {
int A[] = {0, 9999, 8888, 7777, 6666, 5555};
HEAPSORT (A);

for (int i = 1; i <= 5; ++i)


printf ("%d\n", A [i]);
21-Apr-17 34
}
Heapsort: Time complexity
MAX_HEAPIFY takes O(n) time
n BUILD_HEAPs each take O( log2n ) time
Best, worst, average-case
O(nlog2n)

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

You might also like