You are on page 1of 7

Sorting

Heap Sort
 In this sorting algorithm we first build a heap using the
given elements
 If we want to sort the elements in ascending order we
create a Min Heap
 And to sort the element in descending order we create a Max
Heap
 Once the heap is created we delete the root node from the
heap and put the last node in the root position and repeat the
steps till we have covered all the elements

2
Heap Sort
Note: when sorting in ascending pattern

1). Convert the array to heap


2). Transform the heap into min heap
3). Delete the root node
4). Put the last node of heap in root position
5). Repeat from step 2 till all nodes are covered

3
Example
Sort the following given in ascending order
25 53 74 51 45 68 63 79 35 33

4
Heap Sort
Note: when sorting in descending pattern

1). Convert the array to heap


2). Transform the heap into max heap
3). Delete the root node
4). Put the last node of heap in root position
5). Repeat from step 2 till all nodes are covered

5
Example
Sort the following given in Descending order
51 78 2 76 45 71 68 52 39 57

6
Activity:
Sort the given below in ascending and descending
using Heap Sort

23 2 8 14 25 11 12 0

8 15 0 33 13 1 42 5 39 56

You might also like