0% found this document useful (0 votes)
78 views20 pages

Heap Sort

The document discusses heap sort, a sorting algorithm that uses a heap data structure. It begins by defining a heap as a nearly complete binary tree where each node's value is less than or equal to its parent's. A heap can be either a max heap, where the root contains the largest value, or a min heap with the smallest root value. The document then explains how to create a heap from an unsorted array, insert new nodes, delete the root node, and reform the heap after each deletion to maintain the heap property. Finally, it notes that while heap sort has O(n log n) time complexity like other common sorting algorithms, it is preferable for extremely large data sets due to its non-recursive nature.

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views20 pages

Heap Sort

The document discusses heap sort, a sorting algorithm that uses a heap data structure. It begins by defining a heap as a nearly complete binary tree where each node's value is less than or equal to its parent's. A heap can be either a max heap, where the root contains the largest value, or a min heap with the smallest root value. The document then explains how to create a heap from an unsorted array, insert new nodes, delete the root node, and reform the heap after each deletion to maintain the heap property. Finally, it notes that while heap sort has O(n log n) time complexity like other common sorting algorithms, it is preferable for extremely large data sets due to its non-recursive nature.

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 20

DATA

STRUCTURES

MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
[email protected]

(C) GOYANI MAHESH 1


HEAP SORT

(C) GOYANI MAHESH 2


HEAP SORT

 A heap is defined as an almost complete binary tree of n nodes such that


the value of each node is less than or equal to its father.

 A [j] <= A [j-1] / 2

 The root of the binary tree holds the largest key in the heap. This type of
heap is usually called Descending Heap or Mere Heap or Max Heap.

 We can also define an Ascending Heap as an complete binary tree in which


value of each node is greater than its father. This root node has smallest
element of the heap. This type of heap is also called Min Heap.

 Heap can be used to implement priority Queue.

 Pros: Non-recursive, making it a good choice for extremely large


data sets.

 Cons: Slower than the MERGE and QUICK sorts.

(C) GOYANI MAHESH 3


HEAP CREATION Input Is : 33, 42, 67, 23, 44, 49, 74

74
33
42
67
42

33
67
44
42
33
44 42
49
67
74
49

23 44
33 42
49 49
74
23 74

Heap Is : 74, 44, 67, 23, 33, 42, 49

(C) GOYANI MAHESH 4


INSERT NODE

 Input n Element in Heap H

 Add new node by incrementing size of heap


H : n = n + 1 & LOC = n

 While (LOC < 1), Repeat step 4 to 7

 PAR = LOC / 2

 If (DATA < HA [PAR])

HA [LOC] = DATA

Exit

 HA [LOC] = HAP [PAR]

 HA [1] = DATA

 Exit

(C) GOYANI MAHESH 5


MIN HEAP

10 10

20 80 20 80

40 60 85 99 40 60 85 99

50 700 65 50 700 65 15

(C) GOYANI MAHESH 6


10 10

20 80 15 80

40 60 85 99 40 20 85 99

50 700 65 15 50 700 65 60

(C) GOYANI MAHESH 7


MAX HEAP

16

5 11

3 18
16

18 11

3 5 18

16 11

(C) GOYANI MAHESH 3 5 8


DELETE ROOT

10

20 80

40 60 85 99

50 700 65

(C) GOYANI MAHESH 9


DELETE ROOT

10 65

20 80 20 80

40 60 85 99 40 60 85 99

50 700 65 50 700 65

(C) GOYANI MAHESH 10


DELETE ROOT

65 20

20 80 40 80

40 60 85 99 50 60 85 99

700 50 700 65

(C) GOYANI MAHESH 11


Forming the heap from an unsorted array

11 26 14 2 19 32 7

32
26 19
11 7 14 2

(C) GOYANI MAHESH 12


Populating the new array

32

26 19
11 7 14 2

(C) GOYANI MAHESH 13


Reforming the heap

32

19
26 14
11 7 2

(C) GOYANI MAHESH 14


Reforming the heap

32

26
19 14
11 7 2

(C) GOYANI MAHESH 15


Repeat the process

32 26

19 14
11 7 2

(C) GOYANI MAHESH 16


Repeat the process

32 26

14
19 2
11 7

(C) GOYANI MAHESH 17


Repeat the process

32 26

19
14 2
11 7

(C) GOYANI MAHESH 18


Repeat the process

32 26 19

14 2
11 7

(C) GOYANI MAHESH 19


COMPLEXITY

 The heap sort is the slowest of the O(n log n) sorting algorithms
 But unlike the merge and quick sorts it doesn't require massive recursion
or multiple arrays to work. This makes it the most attractive option for very large
data sets of millions of items.

(C) GOYANI MAHESH 20

You might also like