You are on page 1of 27

Welcome to All

Data Structures and Algorithm


Subject Name : Data Structures and
Algorithm
Subject Code : 16SCCCA5/16SCCCS5/16SCCIT5

Class : III BCA/III B.Sc C.S/III B.Sc IT


Sub. Incharge : Dr.K.Bhuvaneswari
Assistant Professor
Dept. of BCA

Idhaya College for Women


Kumbakonam
Unit – III : Algorithms – Priority Queues - Heaps –
Heap Sort – Merge Sort – Quick Sort – Binary Search –
Finding the Maximum and Minimum.
• Heap - A max (min)heap is a complete binary tree with the
property that the value at each node is at least as large as (as
small as) the values at its children(if they exist). Call this
property the heap property.
Heap Sort
• In this sorting algorithm we first build a heap using given
elements.
• If we want to sort the elements in ascending order we create a
Min Heap.
• And to sort the elements 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 elements.

Example: Sorting in Ascending Order


1. Build Heap
2. Transform the heap into min heap
3. Delete the root node
4. Put the last node of the heap in root position
5. Repeat from step 2 till all nodes are covered.
Input 40 60 10 20 50 30

• There are 6 elements. So our heap will have 6 nodes.


• We can represent the nodes of the heap in an array.

Array Index 0 1 2 3 4 5 6
Input 40 60 10 20 50 30

• Array Index starts from 0


• We can save the elements from index 1.
Build Heap
Create a Min Heap

Here 6 nodes
N=6
Floor(N/2)
= floor(6/2)
= floor (3)
=3
• Min Heap the parent
=> We start comparison from 3
node is always smaller
than or equal to its child
nodes.
• Is there any child node smaller than 10?
No
• So we move to index 2 or the 2nd node.

• Is there any child node smaller than 60?


• Yes its 20
• So we swap position of 20 and 60

• We move to index 1 of the 1st node


• Is there any child node smaller than 40?
• Yes, its 10
• So we swap 10 & 40
• Min Heap – a parent node is always smaller
than or equal to child nodes.
• So we have a min heap.
• Delete the root node
• Now we will take the element at the root
node and put it in our sorted element list

0 1 2 3 4 5 6
Array Index
10
Sorted Element
• Put the last node in the root position.
• Now there are 5 nodes. N = 5
• = floor (N/2)
• = floor (5/2)
• = 2.5
• = 2 (Start from 2 node)
• Any child node smaller than 20?
• No.
• So we move to index 1 or the 1st node

See, parent node are smaller than child node


So we have a min heap

0 1 2 3 4 5 6
Array Index
10 20
Sorted Element
• Put the last node in the root position.
• Now there are 4 nodes. N = 4
• = floor (N/2)
• = floor (4/2)
• = 2 (Start from 2 node)

• Is there any child node smaller than 40?


No
• So we move to index 1st node.
• Is there any child node smaller tha
50?
• Yes its 30
• So we swap position of 30 and 50

Array Index 0 1 2 3 4 5 6
10 20 30
Sorted Element
• Delete the root node
• Put last node in the root node
• 3 nodes N= 3
• = Floor (3/2)
• = Floor (1.5)
• = 1 (So we start from 1)
• Child node smaller than 60?
• Yes its 40
• Swap 40 & 60

Array Index 0 1 2 3 4 5 6
10 20 30 40
Sorted Element

• Put last node parent position


• = floor (2/2)
• = floor (1)
Array Index 0 1 2 3 4 5 6
10 20 30 40 50 60
Sorted Element
• Delete the parent node
• Sorted elements in ascending order.
Merge Sort

• In this Sorting algorithm we use the idea of divide and


conquer. We divide the array into two parts, sort them and
then merge them to get the elements in ascending and
descending order.
• Merge sorting is done recursively.
• We take an array and keep dividing from the middle till we
get only one element in each halves(sub-array)
• Then we sort the sub-arrays and join(merge)them back to
get the final sorted array.
Example
Consider an array arr having 6 elements.

5 4 3 1 2 6

Arrange the elements in ascending order using merge sort


algorithm.
• As both left and right sub array are sorted.
• We just have to merge them in ascending order using
merging algorithm

0 1 2 3 4 5
1 2 3 4 5 6
• Merge Sort follows recursive algorithms
• We divide the array into halves till the sub array has only 1
element
• Major work is done in merging the sub arrays
• We need extra temporary array of the same size as the input
array of merging.
Tree of calls of Merge Sort(l,10)
Thank you

You might also like