0% found this document useful (0 votes)
16 views

Heap Sort

The document discusses heaps and their properties, representation as arrays, operations like insertion and deletion, maintaining the heap property, and applications like priority queues and heapsort. Key topics covered include the structural and heap properties of binary heaps, array implementation, max and min heaps, building and modifying heaps.

Uploaded by

jk
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)
16 views

Heap Sort

The document discusses heaps and their properties, representation as arrays, operations like insertion and deletion, maintaining the heap property, and applications like priority queues and heapsort. Key topics covered include the structural and heap properties of binary heaps, array implementation, max and min heaps, building and modifying heaps.

Uploaded by

jk
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/ 21

Department of Computer Science and Engineering (CSE)

TOPICS TO BE COVERED
• INTRODUCTION TO HEAPS
• ARRAY REPRESENTATION OF HEAPS
• HEAP TYPES
• ADDING/DELETING NODE FROM A HEAP
• MAINTAINING A HEAP PROPERTY

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

The Heap Data Structure


• Def: A heap is a nearly complete binary tree with the
following two properties:
– Structural property: all levels are full, except possibly the last
one, which is filled from left to right
– Order (heap) property: for any node x
Parent(x) ≥ x

7 4
5 2 A heap is a binary tree that is filled in
Heap order
3
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

When heaps are useful?


• Heaps are used when the highest or lowest
order/priority element needs to be removed. They allow
quick access to this item in O(1) time. One use of a heap
is to implement a priority queue.
• Binary heaps are usually implemented using arrays,
which save overhead cost of storing pointers to child
nodes.

University Institute of Engineering (UIE)


Array Representation of Heaps

• A heap can be stored as an


array A.
– Root of tree is A[1]
– Left child of A[i] = A[2i]
– Right child of A[i] = A[2i + 1]
– Parent of A[i] = A[ i/2 ]

5
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Heap of n elements is based on a complete binary tree so its


height is log(n)

In complete binary trees, each level is filled up before


another level is added and the levels are filled from left to
right.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Heap Types

7
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Adding/Deleting Nodes
• New nodes are always inserted at the bottom level (left to
right)
• Nodes are removed from the bottom level (right to left)

8
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Operations on Heaps
• Maintain/Restore the max-heap property
– MAX-HEAPIFY
• Create a max-heap from an unordered array
– BUILD-MAX-HEAP
• Sort an array in place
– HEAPSORT

9
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Construction of heap tree and insertion

• Insert key one by one


• Heapify Method

Insertion in Heap Tree


Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Insertion in Heap Tree

University Institute of Engineering (UIE)


Maintaining the Heap Property
• Suppose a node is smaller than a child
– Left and Right sub trees of i are max-heaps
• To eliminate the violation:
– Exchange with larger child
– Move down the tree
– Continue until node is not smaller than
children

12
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

35 33 42 10 14 19 27 44 26 31

Max Heap

University Institute of Engineering (UIE)


Example

MAX-HEAPIFY(A, 2, 10)

A[2]  A[4]

A[2] violates the heap property A[4] violates the heap property

A[4]  A[9]

Heap property restored


14
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

145,40,25,65,12,48,1,100,27,7,3,45,9,30
Construct Min Heap:

University Institute of Engineering (UIE)


Deleting the root of the Heap

The standard deletion operation on Heap is to delete the


element present at the root node of the Heap. That is if it
is a Max Heap, the standard deletion operation will delete
the maximum element and if it is a Min heap, it will delete
the minimum element.

 Assign the Root R to some variable ITEM


 Replace the deleted node R by last node of tree , so
that tree is complete binary tree not necessarily Heap
Tree.
 Re-Heapify.

16
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Heapsort
• Goal:
– Sort an array using heap representations

• Idea:
– Build a max-heap from the array
– Swap the root (the maximum element) with the last element in the
array
– “Discard” this last node by decreasing the heap size
– Call MAX-HEAPIFY on the new root(Re heapify)
– Repeat this process until only one node remains

17
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Example..
Sort elements through heapsort (max heap)
15,20,7,9,30
81,89,9,11,14,76,54,22

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Advantages of Heap sort


• Efficiency – The time required to perform Heap sort
increases logarithmically while other algorithms may
grow exponentially slower as the number of items to sort
increases. This sorting algorithm is very efficient.
• Memory Usage – Memory usage is minimal because
apart from what is necessary to hold the initial list of
items to be sorted, it needs no additional memory space
to work

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Disjoint Set Data Structure, Augmented data structure---for


self reading

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

References
1. Li`pschutz, Seymour, “Data Structures”, Schaum's Outline Series,
Tata McGraw Hill.
2. Data structure and algorithm by Narasimha Karumanchi.
3. www.tutorialspoint.com
4. www.geeksforgeeks.com

University Institute of Engineering (UIE) 21


Department of Computer and Communication Engineering (CCE)

Books Recommended
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M.,
“Data Structures and Algorithms in C++”, Wiley Student Edition.
• Aho, Alfred V., Ullman, Jeffrey D., Hopcroft ,John E. “Data
Structures and Algorithms”, Addison Wesley
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series,
Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures
using C and C++”, Prentice Hall of India

University Institute of Engineering (UIE)

You might also like