Course Name: Data Structure
MODULE-III
SEARCHING AND SORTING
Dr. Gufran Ahmad Ansari
(Professor)
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
1
MIT WORLD PEACE UNIVERSITY PUNE
CONTENTS MODULE- III
SEARCHING
1. Linear Search and Binary Search (array/binary tree)
2. Sorting: General background of sorting techniques:
Bubble sort, Insertion Sort, Selection Sort, Quick Sort,
Merge sort, Heapsort and Radix Sort
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
2
MIT WORLD PEACE UNIVERSITY PUNE
SEARCHING
The process of finding the location of specific data items or record
with a given key value or finding the location of all records is
called searching.
If the item is the given list then search is said to successful
otherwise if items or not found in the given list the search said to
be unsuccessful.
Searching falls into two categories:
1- External searching
2- Internal searching
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
3
MIT WORLD PEACE UNIVERSITY PUNE
External searching means searching the records using keys where
there are many records which reside in files stored on disk.
Internal search means searching the records using keys where
there are less number of records residing entirely within the
computers main memory.
There are many different searching algorithms. The algorithm
that can chooses generally depends on the way information in
DATA is arranged.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
4
MIT WORLD PEACE UNIVERSITY PUNE
Following are the three important searching technique.
1. Linear or Sequential Search
2. Binary Searching
3. Interpolation search
In the data structures when we use the word searching we
actually refer to internal searching.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
5
MIT WORLD PEACE UNIVERSITY PUNE
The complexity of searching algorithm is measured in terms of
number f(n) of comparisons required to find ITEM in the data
where DATA contains n elements.
The time required for a search operation depends on the
complexity of searching algorithm.
Basically we have to consider three cases when we search for a
particular element in the list.
1- Best Case : the best case is that in which the element is found
during the first comparison.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
6
MIT WORLD PEACE UNIVERSITY PUNE
2- Worst Case : The worst case is that in which the element is
found only at the end.
3- Average case : The average case is that in which the element is
found in the comparisons more than best case but less than worst
case.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
7
MIT WORLD PEACE UNIVERSITY PUNE
LINEAR SEACRH OR SEQUENTIAL SEARCH
In linear search each element of an array is read one-by-one
sequentially and it is compared with desired element and if a
match is found then that particular item is returned.
A search will be unsuccessful if all the elements are read and
the desired element is not found.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
8
MIT WORLD PEACE UNIVERSITY PUNE
LINEAR SEACRH OR SEQUENTIAL SEARCH Cont….
The list given above is the list of elements in an unsorted array.
The array contains 10 elements, suppose the element to be
searched is 46 then 46 is compared with all the elements starting
from the 0th element and searching process ends where 46 is found
or the list ends.
The performance of the linear search can be measured by counting
the comparison done to find out an element.
The number of comparisons is 0(n)
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
9
MIT WORLD PEACE UNIVERSITY PUNE
ALGORITHM
Search(int A[ ], int N, int K) /* A is a array , N is array variable size
{ K is a searching elements*/
for i=0 to N-1
if K = A[i]
then
return -i; /*Position Value*/
else
return -1; /* Unsuccessful Search*/
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
10
MIT WORLD PEACE UNIVERSITY PUNE
IMPLEMENTATION Linear search no logic, no order just
#include<stdio.h> compare element
#include<conio.h> 0 1 2 3 4
main() A[5] = {10,20,30,40,50}
{ Key =30
int A[5]={10,20,30,40,50}; i=0=>Key==a[0]
int Key, Flag=0; 30==10 false
printf( “ Enter Search Key”); Flag=0
i=1=>Key==a[1]
scanf(“%d”, &Key);
30==20 false
for(i=0;i<5;i++) Flag=0
{ if(Flag==1) i=2=>Key==a[2]
If(Key==A[i]) { 30==30 True=1
Flag=1; printf( “ Key is found”); Flag=1
break; else
} printf( “ Key is not found”)
} }
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
11
MIT WORLD PEACE UNIVERSITY PUNE
ADVANTAGES
It can operate sorted and unsorted array.
It is the simplest way for finding the element in a list.
For smaller linear search may be faster because the speed of the
simple increment.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
12
MIT WORLD PEACE UNIVERSITY PUNE
DISADVANTAGES
It is very slow process.
It is used only for small amount of data.
It is very time consuming method.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
13
MIT WORLD PEACE UNIVERSITY PUNE
ANALYSIS OF SEQUENTIAL SEARCH
If the item is not in the list the only way to know it is to compare
it against every item present.
If there n items then the sequential search requires n
comparisons to discover that item is not there.
In the case where the item in the list the analysis is not straight
forward.
There is actually three different scenarios that occur.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
14
MIT WORLD PEACE UNIVERSITY PUNE
In the best case we find the item in the first place we took at the
beginning of the list. We need only one comparison.
In the worst case we will not discover item until the very last
comparison, the nth comparison.
In the average case we will found the item about half into the list
i.e. we will compare against n/2 items.
So the complexity of sequential search is o(n);
Case Best case Worst case Average case
(i) Item present 1 n n/2
(ii) Item is not present n n n
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
15
MIT WORLD PEACE UNIVERSITY PUNE
BINARY SEARCH
Binary search is the most popular search algorithm. It is efficient and
also one of the most commonly used technique that is used to solve
problem.
If we place our item in an array and sort item in either ascending or
descending order on the key first, then we can obtain much better
performance with the algorithm called Binary search.
Binary search is an extremely efficient algorithm when it is compared to
linear search.
In binary search we first compare the key with the item in the middle
position of the array.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
16
MIT WORLD PEACE UNIVERSITY PUNE
BINARY SEARCH Cont…
If there is match, we can return immediately.
If the key is less than the middle key, then the item sought
must lie in the lower half of the array; if it is greater then the
item sought must lie in the upper half of the array.
So we repeat the procedure on the lower (or upper) half of the
array.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
17
MIT WORLD PEACE UNIVERSITY PUNE
GENERAL IDEA ABOUT BINARY SEARCH ALGORITHM
1- Find the middle element of the array (i.e., n/2 is the middle element if
the array or sub-array contains n elements)
2- Compare the middle element with the data to be searched, then there
are following three cases.
(a) If it is a desired element then search is successful.
(b) If it is less than desire data then searched only the first of the array ,
i.e., the elements which come to the left side of the middle element.
(c) If it is greater than the desired data then searched only the second of
the array , i.e., the elements which come to the right side of the middle
element.
Repeat the same step until an element is found the search area.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
18
MIT WORLD PEACE UNIVERSITY PUNE
ALGORITHM
1- Input an array A of n elements an “data” to be sorted.
2- LB=0, UB=n; mid=int((LB+UB)/2)
3- Repeat step 4 and 5 while(LB<=UB) and (A[mid]!=data)
4- If(data<A[mid])
UB=mid-1
else
LB=mid+1
5- Mid=int((LB+UB)/2)
6- If(A[mid]==data)
print “the data is found”
else
print “ the data is not found”
7- Exit
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
19
MIT WORLD PEACE UNIVERSITY PUNE
C Implementation of Binary Search
#include<stdio.h>
#include<conio.h> printf(“\n\n Press any key to continue…”);
getch();
void main()
{
char ch;
int arr[20],start,end,mid,n,i,data;
clrscr();
printf(“\n How many elements want to enter in Array”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\Enter element %d:”, i+1);
scanf(“%d”,&arr[i]);
}
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
20
MIT WORLD PEACE UNIVERSITY PUNE
do
{
clrscr();
printf(“\n Enter the element to be searched”);
scanf(“%d”, &data);
start=0;
end=n-1;
mid=(start+end)/2;
while(data!=arr[mid] && start <=end)
{
if(data>arr[mid])
start=mid+1;
else
end=mid-1;
mid=(start+end)/2;
}
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
21
MIT WORLD PEACE UNIVERSITY PUNE
if(data==arr[mid])
printf(“\n%d found at position %d\n”,data,mid+1);
if(start>end)
printf(“\n %d not found in Array”, data);
printf(“\n\n Press<Y or y to continue”);
fflush(stdin);
scanf(“%c”, &ch);
}
while(ch == ‘Y’ || ch == ‘y’)
}
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
22
MIT WORLD PEACE UNIVERSITY PUNE
INTERPOLATION SEARCH
In interpolation search an ordered array is called interpolation search.
This method is even more efficient than binary search, if the
elements are uniformly distributed(or sorted in an Array A)
In binary search, the search space is always divided in two to
guarantee logarithmic time; however when we search for “Ajay” in
the phone book, we don’t start in the middle- we start toward the
front and work from there. That is the idea of an interpolation search.
Instead of cutting the search space by a fixed half, we cut it by an
amount that seems most likely to be succeed. This amount is
determined by interpolation.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
23
MIT WORLD PEACE UNIVERSITY PUNE
Consider an array A of an elements and the elements are
uniformly distributed(or the elements are arranged in a sorted
array).
Initially as an binary search , low is set to 0 and high is set to
n-1.
Now we are searching an element key in array between A [low]
and A [high]. The Key would be expected to be at mid, which is
approximately position.
mid=low+(high-low)*((key-A[low]/A[high]-A[low]))
If key is lower than A[mid], reset high to mid-1; else reset to
low to mid+1.
Repeat the process until they key has found or low>high
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
24
MIT WORLD PEACE UNIVERSITY PUNE
ALGORITHM
Suppose A be a array of sorted elements and key is the elements to be searched
and low represents lower bound of the array and high represent the higher
bound of the array.
1- Input a sorted array of an element and the key to be searched.
2- Initialise low=0 and high=n-1
3- Repeat the steps 4 through the 7 until if(low<high)
4- Mid=low+(high-low)*((key-A[low]/(A[high]-A[low]))
5- If(key==A[mid])
print” Key is found”
6- If(key<A[mid])
high=mid-1
else if(key>A[mid])
low=mid+1
else
print “the key is not in the array and Exit”
7- Stop
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
25
MIT WORLD PEACE UNIVERSITY PUNE
EXAMPLE
Let us consider 7 numbers as
4, 15, 20, 35, 45, 55, 65
Solutions: Suppose we are searching 45 from the given array.
Here n=7, key 45, low =0, high = n-1=6
mid = 0 + (6 - 0) * {(45 - 4) / (65-4)}
= 0 + 6 * (41/61)
= 4.032
Consider only the integer part of the mid i.e., mid = 4
if(key==A[mid])
i.e. key == A[4]
45 = 45
Thus the key is found.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
26
MIT WORLD PEACE UNIVERSITY PUNE
SORTING
Sorting is nothing but arranging the data in ascending or
descending order. The term sorting come into picture as human
realised the importance of search quickly. For example searching
a particular record in database, roll no in database a page no in a
book etc.
Sorting means retrieval of information is made easier when it is
sorted in some predefined order
There are two basic categories of sorting methods.
1. Internal Sorting
2. External Sorting
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
27
MIT WORLD PEACE UNIVERSITY PUNE
1. Internal Sorting
Sorting of data elements in main memory.
2. External Sorting
When the data to be sorted is so large that some of the data is
present in the main memory and some kept in the secondary
memory.
The various internal sorting methods are:
(1) Bubble Sort
(2) Selection Sort
(3) Insertion Sort
(4) Merge Sort
(5) Quick Sort
(6) Heap Sort
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
28
MIT WORLD PEACE UNIVERSITY PUNE
Bubble Sort
Bubble sort is simple algorithm which is used to sort a given set of
n elements provided in form of an array with n number of
elements.
Bubble sort compares all the elements one by one and sort them
based on their values.
If the given array has to be sorted in ascending order then bubble
sort will start by comparing the first elements of the array with
the second element.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
29
MIT WORLD PEACE UNIVERSITY PUNE
If first element is greater than the second element it will swap
both the element and then move on to compare the second
and third element and so on
If we have total n elements then we need to repeat this
process for n-1 times.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
30
MIT WORLD PEACE UNIVERSITY PUNE
IMPLEMENTING BUBBLE SORT ALGORITHM
Following are the steps involved in bubble sort for sorting a given array in
ascending order.
1- Starting with the first element (Index=0) compare the current element
with the next element of the array.
2- If the current element is greater then the next of the element of the
array, swap them
3- If the current element is less than the next element, move the next
element repeat step -1
Let us consider and Array with values { 5, 1, 6, 2, 4, 3}
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
31
MIT WORLD PEACE UNIVERSITY PUNE
Let us consider and Array with values { 5, 1, 6, 2, 4, 3}
Now see pictorial representation of bubble sort given array below;
5>1 So interchange 5 1 6 2 4 3
5<6 No swapping 5 1 6 2 4 3
6>2 So interchange 1 5 6 2 4 3
6>4 So interchange 1 5 2 6 4 3
6>3 So interchange 1 5 2 4 6 3
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
32
MIT WORLD PEACE UNIVERSITY PUNE
1 5 2 4 3 6
This is first insertion, similarly after all the insertions the array get sorted.
1 2 3 4 5 6
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
33
MIT WORLD PEACE UNIVERSITY PUNE
SELECTION SORT ALGORITHM
Selection sort is conceptually the most simplest sorting
algorithm.
This algorithm will first find the smallest element in the
array and swap it with the element in the first position.
Then it will find the second smallest element and swap it
with the element in the second position and it will keep
doing this until the entire array is sorted.
It is called selection sort because it repeatedly selects the
next smallest element and swaps it into right place.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
34
MIT WORLD PEACE UNIVERSITY PUNE
Steps involved in Selection Sort
For sorting a given array in ascending order.
1- Starting from the first element, we search the smallest element in
the array and replace it with the element in the first position.
2- We move on the second position and look for smallest element
present in the sub-array. Starting from index 1 till the last index.
3- We replace the element at the second position in the original array or
we can say at the first position in the sub-array
4- This is repeated until the array completely sorted.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
35
MIT WORLD PEACE UNIVERSITY PUNE
Let us consider an array with values { 3, 6, 1, 8, 4, 5, }
Given figure below shows pictorial representation of how selection sort
will sort the given array.
Original After After Second
Array first Pass Pass
3 1 1
6 6 3
1 3 6
8 8 8
4 4 4
5 5 5
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
36
MIT WORLD PEACE UNIVERSITY PUNE
In the first pass the smallest element will be 1. So it will be placed at the
first position.
The leaving the first element next smallest element will be searched
from the remaining element. We will get the 3 as the smallest so it will
be place at the second position.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
37
MIT WORLD PEACE UNIVERSITY PUNE
The leaving 1 and 3 ( they are at the correct position) we will
search the next smallest elements from the rest of the
elements and put in third position and keep doing this until
array is sorted.
Note:
Selection sort is unstable sort i.e. it might change occurrence of
the two similar elements in the list while sorting. But it can
also work as a stable sort when it is implemented using link list.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
38
MIT WORLD PEACE UNIVERSITY PUNE
INSERTION SORT
Insertion sort works just like its name suggest. It insert each element its
proper place in the final list.
The simplest implementation requires two list structure the source list
and the list into which sorted items are inserted.
During the first iteration, the element position second is compared with
the element at the first position. During the second iteration the
element at the third position is compared with the element at the
second and first position.
This process is repeated all the element in the array up to (n-1) iteration.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
39
MIT WORLD PEACE UNIVERSITY PUNE
EXAMPLE
Let us take the following value
40 50 12 30 90 18 06 60
Here are 8 values , So 7 iteration are required to sort these values.
First Iteration
40 50 12 30 90 18 06 60
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
40
MIT WORLD PEACE UNIVERSITY PUNE
Second Iteration
40 50 12 30 90 18 06 60
12 40 50 30 90 18 06 60
Third Iteration
12 40 50 30 90 18 06 60
12 30 40 50 90 18 06 60
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
41
MIT WORLD PEACE UNIVERSITY PUNE
Fourth Iteration
12 30 40 50 90 18 06 60
Here 90 is greater with all elements before it so no change occurs.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
42
MIT WORLD PEACE UNIVERSITY PUNE
Fifth Iteration
12 30 40 50 90 18 06 60
12 18 40 50 90 30 06 60
12 18 30 50 90 40 06 60
12 18 30 40 90 50 06 60
12 18 30 40 50 90 06 60
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
43
MIT WORLD PEACE UNIVERSITY PUNE
Sixth Iteration
12 18 30 40 50 90 06 60
06 18 30 40 50 90 12 60
06 12 30 40 50 90 18 60
06 12 18 40 50 90 30 60
06 12 18 30 50 90 40 60
06 12 18 30 40 90 50 60
06 12 18 30 40 50 90 60
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
44
MIT WORLD PEACE UNIVERSITY PUNE
Seventh Iteration
06 12 18 30 40 50 90 60
Here is 60 is greater than all the values placed before 90.
.
06 12 18 30 40 50 60 90
This is our sorted Array
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
45
MIT WORLD PEACE UNIVERSITY PUNE
INSERTION ALGORITHM
Step 1 : for j<-2 to length[A]
Step 2 : do key <-A[j] /* Insert A[j] into the sorted sequence
A[1…j-1]*/
Step 3 : i<- i-1
Step 4 : while i>0 and A[i] > Key
Step 5 : do A[i+1]<-A[i]
Step 6 : i<- i-1
Step 7 : A[i+1]<-key
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
46
MIT WORLD PEACE UNIVERSITY PUNE
EXAMPLE
Illustrate the operation of insertion sort on the
Array A = [ 2, 13, 5, 18, 14 ]
SOLUTION
A [ ] = [ 2, 13, 5, 18, 14 ]
n= 5
For j=2 to 5
Now j=2, Key = A[2] i.e. key =13, i=2-1 i.e. i =1
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
47
MIT WORLD PEACE UNIVERSITY PUNE
while i >0 and A[1] > 13
condition false. So no change.
Now j=3, key[3] = 5
i =3-1=2
i=2 and key = 5
while i > 0 and A[2]>key
condition is TRUE
So A[2+1] <- A[2]
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
48
MIT WORLD PEACE UNIVERSITY PUNE
A[3]<- A[2]
i.e. 2 13 18 14
and i=2-1=1, i=1
while 1>0 and A[1] >key
condition false. So no change
Then
A[1+1] <- Key
A[2]<-5
2 5 13 18 14
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
49
MIT WORLD PEACE UNIVERSITY PUNE
For j=4
key = A[4]
Key = 18, i = 3;
now while 3>0 and A[3]>18
condition is false No Change
Similarly j=5
key = A[5]
so key = 14, i =4
Now while 4>0 and A[4]>14
condition true
So A[5]=18 and i=4-1=3
Now while 3>70 and A[3]>14
condition is false
So A[3+1]=A[4]=14
and the sorted array is
A[]= 2 5 13 14 18
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
50
MIT WORLD PEACE UNIVERSITY PUNE
PROGRAM INSERTION SORT
#include<stdio.h>
#include<conio.h>
void insertion ( int x [ ], int n)
{
int I, j, temp;
for(i=0; i<n; i++)
{
temp = x[i];
for(j=i-1; j>=0; j--)
{
if(temp<x[j])
x [ j+1] = x[ j ];
else
break;
}
x[ j+1]=temp; }
}
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
51
MIT WORLD PEACE UNIVERSITY PUNE
void main()
{
int x[10], n, i;
clrscr();
printf(“Enter the no of element:”);
scanf(“%d”, &n);
printf(“\n Enter the elements: \n”);
for(i = 0; i<n; i++)
scanf(“%d”, &x[i]);
insertion(x,n);
printf(“\n Enter the sorted output: \n”);
for(i = 0; i<n; i++)
printf(“\n %d”, x[i]);
getch();
}
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
52
MIT WORLD PEACE UNIVERSITY PUNE
MERGE SORT
Merge sort is a sorting technique based on divide and conquer
technique.
Merge sort first divide the array into two equal halves or
approximately equal and then combines in a sorted order.
How Merge Sort Works?
To understand merge sort, we take an unsorted array as the
following;
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
53
MIT WORLD PEACE UNIVERSITY PUNE
We know that merge sort first divides the whole array
iteratively into equal halves unless the atomic values are
achieved.
We see here that an array of 8 items is divided into two arrays
of size 4.
This does not change the sequence of appearance of items in
the original. Now we divide these two arrays into halves.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
54
MIT WORLD PEACE UNIVERSITY PUNE
We further divide these arrays and we achieve atomic value
which can no more be divided.
Now, we combine them in exactly the same manner as
they were broken down
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
55
MIT WORLD PEACE UNIVERSITY PUNE
We first compare the element for each list and then combine
them into another list in a sorted manner.
We see that 14 and 33 are in sorted positions. We compare 27
and 10 and in the target list of 2 values we put 10 first, followed
by 27.
We change the order of 19 and 35 whereas 42 and 44 are
placed sequentially.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
56
MIT WORLD PEACE UNIVERSITY PUNE
In the next iteration of the combining phase, we compare lists
of two data values, and merge them into a list of found data
values placing all in a sorted order.
After the final merging, the list should look like this −
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
57
MIT WORLD PEACE UNIVERSITY PUNE
ALGORITHM
Merge sort keeps on dividing the list into equal halves until it can no
more be divided. By definition, if it is only one element in the list, it is
sorted.
Then, merge sort combines the smaller sorted lists keeping the new list
sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more
be divided.
Step 3 − merge the smaller lists into new list in sorted order.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
58
MIT WORLD PEACE UNIVERSITY PUNE
Quick Sort
Quick sort is also based on the concept of divide and conquer, just like merge
sort. But in quick sort all the heavy lifting (Major work) is done while dividing
the array into sub array.
While in the merge sort all the real work happens during merging the sub
array.
In this case of quick sort the combine step does absolutely nothing. It is called
partition exchange sort.
This algorithm divides the list into three main parts:
1. Element than the Pivot element
2. Pivot element (Central Element)
3. Element greater than the Pivot element
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
59
MIT WORLD PEACE UNIVERSITY PUNE
Pivot element can be any element from the array. It can be the first
element, the last element or any random element.
In the given example below we will take the rightmost element or the last
element as Pivot.
Example :
In the Array { 52, 37, 63, 14, 17, 8, 6, 25} we take 25 as a Pivot. So after
that the first pass the list will be changed like this
{ 6, 8, 17, 14, 25, 63, 37, 52}
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
60
MIT WORLD PEACE UNIVERSITY PUNE
Hence after the first pass Pivot will set at its position with all the
elements smaller to it on its left side and all the larger than to its right .
Now
{ 6, 8, 17, 14, and 63, 37, 52}
are considered as to separate sub array and same recursive logic will be
applied on them and will keep doing this until the complete array is sorted
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
61
MIT WORLD PEACE UNIVERSITY PUNE
How Quick Sort works?
Following are the steps involved in quick sort algorithm:
1. After selecting an element as Pivot which is the last index of the array
in our case we divide the array first time.
2. In quick sort we call this partitioning. It is not simple breaking down
of array into two sub-arrays.
3. But in case of partitioning the array element also positioned that all
the element smaller than the Pivot will be on left side of the Pivot
and all the elements greater than Pivot will be on right side of it.
4. And the Pivot element will be at its final sorted position.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
62
MIT WORLD PEACE UNIVERSITY PUNE
4. The elements to the left and right side may not be sorted.
5. The we pick subarray element on the left of the Pivot and element
right of the Pivot and we perform partitioning on them by choosing a
Pivot in the sub array.
Let us consider an array with values below;
9 7 5 11 12 2 14 3 10 6
We have pictorial representation how quick sort will sort given above
array
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
63
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
64
MIT WORLD PEACE UNIVERSITY PUNE
In Step-1 , we select last element as the Pivot is 6 in this case and call
for partitioning.
Hence re –arranging the array in such way that 6 will placed in its final
position and to its left will be all the element less than it and to its
right will have all the elements greater than it.
Then we pick the subarray on the left and the subarray right and select
a Pivot for them.
In the previous slide we choose 3 as Pivot for left subarray and 11 for
the right subarray and we will again call partitioning.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
65
MIT WORLD PEACE UNIVERSITY PUNE
Heap Sort
Heap sort is achieved with the help of selection type of sorting
logic along with heap attributes of the array.
Heap data structure is a binary tree with some special structural
orientation often known as complete binary tree.
Logic of heap data structure and heap attribute of an array is used
in heap sorting.
To understand heap we need to understand binary tree and some
related terms. Let us understand these one by one.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
66
MIT WORLD PEACE UNIVERSITY PUNE
Binary Heap
Heap is a tree-based data structure in which all the tree nodes are in a
particular order, such that the tree satisfies the heap properties (that is,
there is a specific parent-child relationship that is followed throughout
the tree).
A heap data structure where the tree is a complete binary tree is
referred to as a binary heap.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
67
MIT WORLD PEACE UNIVERSITY PUNE
A complete binary tree is a binary tree in which all levels except
the bottom-most level are completely filled, and all nodes in the
bottom-most level are as far left as possible. (The last level may
or may not be completely filled.)
A full binary tree is a binary tree where every node has 0 or 2
children.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
68
MIT WORLD PEACE UNIVERSITY PUNE
Properties of a Binary Heap
1. They are complete binary trees: This means all levels are totally filled
(except maybe the last level), and the nodes in the last level are as left as
possible.
This property makes arrays a suitable data structure for storing binary heaps.
We can easily calculate indices of a node’s children. So, for parent index i, the
left child will be found at index 2*i+1, and the right child will be found at index
2*i+2 (for indices that start with 0). Similarly, for a child at index i, its parent
can be found at index i/2.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
69
MIT WORLD PEACE UNIVERSITY PUNE
2. Heaps are typically of two types — max heap and min heap:
In a max heap, the value of a node is always greater than or
equal to the value of each of its children.
Conversely, in a min heap, the value of a parent is always <= the
value of each of its children.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
70
MIT WORLD PEACE UNIVERSITY PUNE
3. In a max heap, the element at the root will always be the
maximum.
In a min heap, the element at the root will always be the
minimum.
Heap sort algorithm takes advantage of this property to sort an
array using heaps.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
71
MIT WORLD PEACE UNIVERSITY PUNE
Heap Sort Definition
Heap sort is an efficient comparison-based sorting algorithm
that creates a heap from the input array and then sorts the
array by taking advantage of a heap's properties.
Please keep in mind, since the heap is a tree-based data
structure, this also means that the knowledge of arrays, trees,
binary trees, and heaps is key to understanding the heap sort
algorithm.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
72
MIT WORLD PEACE UNIVERSITY PUNE
Application of Heap Sort
Heap is used to construct a priority queue.
Heap sort is one of the fastest sorting algorithms with time
complexity of O(N* log(N), and it’s easy to implement.
Best First Search (BFS) is an informed search, where unlike
the queue in Breadth-First Search, this technique is
implemented using a priority queue.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
73
MIT WORLD PEACE UNIVERSITY PUNE
Heap Sort Algorithm
Sorting can be in ascending or descending order. Either Max heap
or min heap logic can be taken depending on the need.
Build a max/min heap using Heapify() from the input data.
At this point, the largest/smallest item is stored at the root of
the heap. Replace it with the last item of the heap followed by
reducing the size of heap by 1. Finally, heapify the root of tree.
Repeat above steps while size of heap is greater than 1.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
74
MIT WORLD PEACE UNIVERSITY PUNE
Heap Sort Step Diagram
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
75
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
76
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
77
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
78
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
79
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
80
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
81
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
82
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
83
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
84
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
85
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
86
MIT WORLD PEACE UNIVERSITY PUNE
Radix Sort Algorithm
Radix sort is one of the sorting algorithms used to sort a list of integer
numbers in order.
In radix sort algorithm, a list of integer numbers will be sorted based
on the digits of individual numbers.
Sorting is performed from least significant digit to the most significant
digit.
Radix sort algorithm requires the number of passes which are equal to
the number of digits present in the largest number among the list of
numbers.
For example, if the largest number is a 3 digit number then that list is
sorted with 3 passes.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
87
MIT WORLD PEACE UNIVERSITY PUNE
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
88
MIT WORLD PEACE UNIVERSITY PUNE
Advantages of Radix Sort Algorithm
1.The radix sort performs better and faster than the quick sort.
2.The radix sort is a stable sort to classify the numbers.
3.The radix sorts maintain equal values.
4.The radix sort is provided an easy and simple algorithm.
5.This algorithm does not work in comparison operation with a
list of numbers.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
89
MIT WORLD PEACE UNIVERSITY PUNE
Disadvantages of Radix Sort Algorithm
1.The radix sort contains a large space for sorting the numbers.
2.The radix sort does not apply to all data types’ values. You apply
only integral values.
3.The radix sort algorithm does not provide better efficiency.
4.This algorithm does not use for tightly clustered values.
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
90
MIT WORLD PEACE UNIVERSITY PUNE
THANK YOU
SCHOOL OF COMPUTER SCIENCE, FACULTY OF SCIENCE
91
MIT WORLD PEACE UNIVERSITY PUNE