KEMBAR78
Sorting algorithms in Data Structure | PPT
Sorting Algorithms
M.Balamurugan
Assistant Professor
Department of Computer Science
Sri Kaliswari College
Sivakasi
AGENDA
INSERTION SORTING
QUICK SORTING
MERGE SORTING
HEAP SORTING
INSERTION SORT
For Insertion sort, we need to consider an array in
two parts – Sorted part & Unsorted part !
Let us consider an unsorted array of integer with 6
elements : 7 2 4 1 5 3
This is an unsorted array
We will have to divide this array into sorted part
and unsorted part
The first element is always sorted
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
What is Insertion Sort and How it works ?
QUICK SORT
Quicksort is an algorithm of divide and conquer type . It is an
algorithm design based on multi-branched recursion . It works
by recursively breaking down a problem into two or more sub
problems of the same related type.
In quick sort we will divide the problems in two sub list.And the
algorithm will find the final position of one of the numbers.
From this position the value of the left side will be less than the
numbers and the value will be greater than the right
EXAMPLE:
SELECT THE PIVOT ELEMENT
What is Merge Sort
Merge Sort is one kind of sorting
that based on Divide and
Conquer algorithm .
How IT WORKS
It divides input array in two halves , calls itself
(using recursive function) for the two halves
and then merges the two sorted halves .
Merge sort
Simulation
Unsorted element 
56 32 16 23 9 17 3 21
Dividing the array
56 32 16 23 9 17 3 21
56 32 16 23 9 17 3 21
56 32 16 23 9 17 3 21
56 32 16 23 9 17 3 21
Merging the array
56 32 16 23 9 17 3 21
5632 16 23 9 17 3 21
16 23 32 56 3 9 17 21
3 9 16 17 21 23 32 56
Sorted element 
3 9 16 17 21 23 32 56
Heap tree is a complete binary tree but
at lowest level of it is not necessary to
make heap tree a complete binary tree.
complete binary tree
Not necessray
Heap Sort
Heap Sort
•:Length: Number of elements in the
array
•Heap-size - how many elements in
the heap are stored within
array A.
Tree (Binary tree)
Arrays
TWO WAYS TO IMPLEMENT
HEAP-SORT:
 Maintaining heap property (min
or max)
 Maintaining shape property
(complete binary tree)
STEPS TO BUILD HEAP
MAINTAINING HEAP PROPERTY
Each node in a tree has a key which is
more extreme (greater or less) than or
equal to the key of its parent.
 A complete tree with the heap property is
a heap.
MAX-HEAP PROPERTY
Each node in a tree has a key which is
less than or equal to the key of its parent.
A[PARENTi] >= A[i]
“While using this property it makes a list of
descending order.”
MIN-HEAP PROPERTY
Each node in a tree has a key which is
greater than or equal to the key of
its parent.
A[PARENTi] <= A[i]
“While using this property it makes a list of
ascending order.”
BUILDING THE HEAP
“ The Build heap method
shown in Program transforms an
unsorted array into a max
heap or min heap ”
EXAMPLE (MAX-HEAP & LEFT ALIGNED)
15
10
177
19
16
0 1 2 3 4 5
15
10
17
19
1677
16
15
17
19
1077
16
15
17
19
1077
16
15
17
19
10
77
16
15
17
19
10
77
16
19
177
15
10
7
16
19
15
17
10
77
16
10
15
17
19
77
SWAPPING
16
10
15
17
77
10,17,16,7,15,19
DELETION
16
17
15
10
77
17,10,16,7,15,19
HEAPIFY
16
17
10
15
77
17,15,16,7,10,19
HEAPIFY
16
10
17
15
77
10,15,16,7,17,19
SWAPPING
16
10
15
77
10,15,16,7,17,19
DELETION
10
16
15
77
16,15,10,7,17,19
HEAPIFY
10
7
15
16
7,15,10,16,17,19
SWAPPING
10
7
15
7,15,10,16,17,19
DELETION
10
15
7
15,7,10,16,17,19
HEAPIFY
10
10,7,15,16,17,19
7
SWAPPING
DELETION
7
7,10,15,16,17,19
SWAPPING
DELETION

Sorting algorithms in Data Structure