0% found this document useful (0 votes)
2 views10 pages

Lab Program 4 Heapsort

The document explains the concept of heaps and heapsort, detailing the properties of max and min heaps, and the process of bottom-up heap creation. It outlines the heapsort algorithm, which involves constructing a heap from an array and then sorting it by repeatedly exchanging the root with the last leaf and re-heapifying. The time complexity of the heap creation process is O(n).

Uploaded by

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

Lab Program 4 Heapsort

The document explains the concept of heaps and heapsort, detailing the properties of max and min heaps, and the process of bottom-up heap creation. It outlines the heapsort algorithm, which involves constructing a heap from an array and then sorting it by repeatedly exchanging the root with the last leaf and re-heapifying. The time complexity of the heap creation process is O(n).

Uploaded by

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

LAB PROGRAM-4

HEAP SORT
HEAPS and HEAPSORT
• A heap is a binary tree with keys assigned to its nodes (one key
per node) provided the following two conditions are met:
1. The binary tree is essentially complex. i.e., all its levels are full except
possibly the last level where only the rightmost leaves may be missing.
2. The key at each node is greater than or equal to the keys at its children.

Not a heap because 1st condition is not


satisfied

Not a heap because 2nd condition is not


satisfied
Parent (5) < child (6)

HEAP
HEAP
• Max heap (Descending Heap) is almost complete
binary tree such that an item at any given node >= (left
child and right child).
• Min heap (Ascending Heap) is almost complete
binary tree such that an item at any given node <= (left
child and right child). 32
Max Heap Min Heap
because because
parent 67 72 parent
>=child <=child
76 85 89
PROPERTIES OF MAX HEAP
• There exists exactly one essential complete binary tree with ‘n’
nodes . Its height = log2n.
• Root of a heap always contains its largest element
• A node of a heap considered with all its descendants is also a
heap.
• A heap can be implemented in an array H by recording its
elements in(p)the top-down,
0 left-to-right
1 2 fashion
3 4 leaving
5 H[0]
6
1
unused. 72 72 67 45 34 42 40
(2p (2p+
2) 3 1)
67 45 Parents Leaves

4
34 42 40

5 6
BOTTOM-UP HEAP CREATION
• It initializes the essentially complete binary tree with n nodes by placing keys in
the order given and then “heapifies” the tree as follows.
• Starting with the last parental node, the algorithm checks whether the parental
dominance holds for the key in this node.
• If it does not, the algorithm exchanges the node’s key K with the larger key of its
children and checks whether the parental dominance holds for K in its new
position.
• This process continues until the parental dominance for K is satisfied.
• After completing the “heapification” of the subtree rooted at the current parental
node, the algorithm proceeds to do the same for the node’s immediate predecessor.
The algorithm stops after this is done for the root of the tree.
Bottom-Up Heap Algorithm
31 21 42 14 16 12

Time complexity is O(n) where n is the


number of elements
1) Construct heap using bottom-up heap method for
2, 9, 7, 6, 5, 8
HEAPSORT
• Step-1) Construct heap for the given array
• Step-2) Delete root item and insert in appropriate position and repeat
this (n-1) times. Exchange root item with last leaf.
Algorithm HeapSort (n, a[])
// n is the number of items, a is array, output is sorted list
Begin
HeapBottomUp(n, a) // Create Heap using bottom up method
for i = (n-1) downto 0
Exchange a[0], a[i] // Exchange root with leaf
HeapBottom(i, a) // Recreate heap from root
end for
End
1) Sort 100, 75, 80, 25, 50, 30, 45 using heapsort

(Already a max
heap so exchange
root 100 with leaf
45)

(Sorted array)
SAMPLE INPUT AND OUTPUT OF HEAPSORT

You might also like