Unit 2 Serching and Sorting
Unit 2 Serching and Sorting
Iteration 1
10 20 30 40 50
10 ! = 30
Index = Index + 1
Iteration 2
10 20 30 40 50
20 ! = 30
Index = Index + 1
Iteration 3
10 20 30 40 50
30 = 30
Number found
a[]={13,15,18,20,25}
Sandip Polytechnic,Nashik
2
UNIT II-SERCHING AND SORTING
Example: array[]
Index 0 1 2 3 4 5 6 7
elements 20 35 15 5 12 27 10 3
search element:5
step 1: start
step 2 : Read value of search element : consider integer variable x=5
step3 : compare 5 with first element in list/array i.e. 20 35 15 5 12 27 10 3
Both(search element and first element) are not matching
Step4: compare 5 with second element in list/array
20 35 15 5 12 27 10 3
Both r not matching
Step 5: compare
20 3515 5 12 27 10 3
Step 6: compare 5 with 5
Both are matching
Print message “element found at index 3.”
Step 7: STOP
#inlclude<stdio.h>
#include<conio.h>
void main()
{
int search;
int a[10]={20, 35 ,15, 5 ,12, 27, 10 ,3 };
printf(“Enter element to search:”);
scanf(“%d”,&search);
Sandip Polytechnic,Nashik
3
UNIT II-SERCHING AND SORTING
for(i=0;i<10;i++)
{
if(search==a[i]) a[3]
{
printf(“Element found at location %d in array”, i);
break;
}
}
if (i==10)
printf(“Element not available in list/array”);
} // 5== a[0]20
Binary search:
2. Binary search:
- “It is a searching technique which is applied on sorted list/Data
structure.”
- In this technique searching takes place with the help of Middle
element.
- Compare element to be search with mid element
- If element to search is less than mid element then search in
sublist 1/left sublist of the middle element
- If element to be search is greater than mid element then
search(Binary search) in sublist 2/right sublist from the middle
element.
Sandip Polytechnic,Nashik
4
UNIT II-SERCHING AND SORTING
Sublist1 sublist 2
Algorithm
Step 1: Start
Step 2 :Read list of elements
Step3 : assign low=0,high=n
Step 4: find middle element with (low+high)/2
Step 5: Compare element to be searched with middle element
Step 6: if(search_element<middle_element)
Then
Low=0 , high =(mid-1)
Else if(search_element=middle_element)
“Element found”
Else
Low=mid+1, high=n; mid=(low+high)/2 i.e. sublist 2
Step 7: repeat step 5 & 6 till element found or all possible sub lists
checked
Example 2:
An array which is given A[ ]= {11,5,21,3,29,17,2,43} is not in sorted manner,
first we need to sort them in order;
So an array will be A[ ]={2,3,5,11,17,21,29,43} and the value to be searched is
VAL = 29.
The binary search algorithm will proceed in the following manner
Iteration 1:
low = 0, high = 7, MID = (0 + 7)/2 = 3
Now, VAL = 29 (element to be searched) and A[MID] = A[3] =11
Sandip Polytechnic,Nashik
6
UNIT II-SERCHING AND SORTING
A[3] is less than VAL, therefore, we now search for the value in the second
half
of the array.
So, we change the values of low and MID.
Iteration 2:
Now, low = MID + 1 = 4, high = 7,
MID = (4 + 7)/2 =11/2 = 5;
VAL = 29 and A [MID] = A [5] = 21
A[5] is less than VAL, therefore, we now search for the value in the second half
of the segment.
So, again we change the values of low and MID.
Iteration 3:
Now, low = MID + 1 = 6, high = 7,
MID = (6 + 7)/2 = 6 Now,
VAL = 29 and A [MID] = A [6]=29
Sorting :
Technique/operation of arranging data in ascending or descending
order is called sorting.
i. Internal :
which is applied on data present on main memory
Ex: 1.Bubble sort:
2 Selection sort:
3 Quick sort
4 Radix sort
ii.External sorting:
which applied on data not completely available main memory
5 Merge sort
Bubble sort:
Sandip Polytechnic,Nashik
8
UNIT II-SERCHING AND SORTING
- This algorithm is not suitable for large data sets as its average and worst
case complexity is of Ο(n2) where n is the number of items.
Example 1:
Consider List/ array : 33, 15, 10, 12, 25, 20, 27, 3
Pass 1: compare 33 & 15
15 33 10 12 25 20 27 3
Compare 33 & 10
15 10 33 12 25 20 27 3
Compare 33 & 12
15 10 1233 25 20 27 3
Compare 33 & 25
15 10 12 25 33 2 0 27 3
Compare 33 & 20
15 10 12 25 20 33 27 3
Compare 33 & 27
15 10 12 25 20 27 33 3
Compare 33 & 3
15 10 12 25 20 27 3 33
Pass 2: compare 15 & 10
10 15 12 25 20 27 3 33
compare 15 & 12
10 1215 25 20 27 3 33
compare 15 & 25
10 12 15 25 2027 3 33
Compare 25 & 20
10 12 15 20 25 27 3 33
Compare 25 & 27
10 12 15 20 25 27 3 33
CompRE 27 & 3
10 12 15 20 25 3 27 33
Compare 27 & 33
10 12 15 20 25 3 27 33
Pass 3 :
10 12 15 20 3 25 27 33
Sandip Polytechnic,Nashik
9
UNIT II-SERCHING AND SORTING
10 12 15 20 3 25 27 33
Pass 4 :
10 12 15 20 3 25 27 33
Compare 20 & 3:
10 12 15 3 20 25 27 33
Pass5 :
10 12 15 3 20 25 27 33
Compare 15 & 3:
10 123 15 20 25 27 33
Pass 6:
Compare 12 & 3:
10 3 12 15 20 25 27 33
Pass 7:
Compare 10 & 3:
3 10 12 15 20 25 27 33
Sorted list: 3 10 12 15 20 25 27 33
2,19,3,7,5,27,31
Pass 1 Completed
Pass 2: 2,19,3,7,5,27,31
2,3,19,7,5,27,31
2,3,7,19,5,27,31
2,3,7,5,19,27,31
2,3,7,5,19,27,31
Pass 2 Completed
Pass 3: 2,3,7,5,19,27,31
2,3,7,5,19,27,31
2,3,5,7,19,27,31
Pass 3 Completed
Pass 4: 2,3,5,7,19,27,31
Pass 4 Completed
Pass 5: 2,3,5,7,19,27,31
Pass 5 Completed
Pass 6: 2,3,5,7,19,27,31
Pass 6 Completed
Enter no of elements: 12
Unsorted Data: 29 35 3 8 11 15 56 12 1 4 85 5
After pass 1 : 29 35 3 8 11 15 56 12 1 4 85 5
After pass 1 : 29 3 35 8 11 15 56 12 1 4 85 5
After pass 1 : 29 3 8 35 11 15 56 12 1 4 85 5
After pass 1 : 29 3 8 11 35 15 56 12 1 4 85 5
After pass 1 : 29 3 8 11 15 35 56 12 1 4 85 5
After pass 1 : 29 3 8 11 15 35 56 12 1 4 85 5
After pass 1 : 29 3 8 11 15 35 12 56 1 4 85 5
After pass 1 : 29 3 8 11 15 35 12 1 56 4 85 5
Sandip Polytechnic,Nashik
11
UNIT II-SERCHING AND SORTING
After pass 1 : 29 3 8 11 15 35 12 1 4 56 85 5
After pass 1 : 29 3 8 11 15 35 12 1 4 56 85 5
After pass 1 : 29 3 8 11 15 35 12 1 4 56 5 85
Pass 2
After pass 2 : 3 29 8 11 15 35 12 1 4 56 5 85
After pass 2 : 3 8 29 11 15 35 12 1 4 56 5 85
After pass 2 : 3 8 11 29 15 35 12 1 4 56 5 85
After pass 2 : 3 8 11 15 29 35 12 1 4 56 5 85
After pass 2 : 3 8 11 15 29 35 12 1 4 56 5 85
After pass 2 : 3 8 11 15 29 12 35 1 4 56 5 85
After pass 2 : 3 8 11 15 29 12 1 35 4 56 5 85
After pass 2 : 3 8 11 15 29 12 1 4 35 56 5 85
After pass 2 : 3 8 11 15 29 12 1 4 35 56 5 85
After pass 2 : 3 8 11 15 29 12 1 4 35 5 56 85
Pass 3
After pass 3 : 3 8 11 15 29 12 1 4 35 5 56 85
After pass 3 : 3 8 11 15 29 12 1 4 35 5 56 85
After pass 3 : 3 8 11 15 29 12 1 4 35 5 56 85
After pass 3 : 3 8 11 15 29 12 1 4 35 5 56 85
After pass 3 : 3 8 11 15 12 29 1 4 35 5 56 85
After pass 3 : 3 8 11 15 12 1 29 4 35 5 56 85
After pass 3 : 3 8 11 15 12 1 4 29 35 5 56 85
After pass 3 : 3 8 11 15 12 1 4 29 35 5 56 85
After pass 3 : 3 8 11 15 12 1 4 29 5 35 56 85
Pass 4
After pass 4 : 3 8 11 15 12 1 4 29 5 35 56 85
After pass 4 : 3 8 11 15 12 1 4 29 5 35 56 85
After pass 4 : 3 8 11 15 12 1 4 29 5 35 56 85
After pass 4 : 3 8 11 12 15 1 4 29 5 35 56 85
After pass 4 : 3 8 11 12 1 15 4 29 5 35 56 85
After pass 4 : 3 8 11 12 1 4 15 29 5 35 56 85
After pass 4 : 3 8 11 12 1 4 15 29 5 35 56 85
After pass 4 : 3 8 11 12 1 4 15 5 29 35 56 85
Sandip Polytechnic,Nashik
12
UNIT II-SERCHING AND SORTING
Pass 5
After pass 5 : 3 8 11 12 1 4 15 5 29 35 56 85
After pass 5 : 3 8 11 12 1 4 15 5 29 35 56 85
After pass 5 : 3 8 11 12 1 4 15 5 29 35 56 85
After pass 5 : 3 8 11 1 12 4 15 5 29 35 56 85
After pass 5 : 3 8 11 1 4 12 15 5 29 35 56 85
After pass 5 : 3 8 11 1 4 12 15 5 29 35 56 85
After pass 5 : 3 8 11 1 4 12 5 15 29 35 56 85
Pass 6
After pass 6 : 3 8 11 1 4 12 5 15 29 35 56 85
After pass 6 : 3 8 11 1 4 12 5 15 29 35 56 85
After pass 6 : 3 8 1 11 4 12 5 15 29 35 56 85
After pass 6 : 3 8 1 4 11 12 5 15 29 35 56 85
After pass 6 : 3 8 1 4 11 12 5 15 29 35 56 85
After pass 6 : 3 8 1 4 11 5 12 15 29 35 56 85
Pass 7
After pass 7 : 3 8 1 4 11 5 12 15 29 35 56 85
After pass 7 : 3 1 8 4 11 5 12 15 29 35 56 85
After pass 7 : 3 1 4 8 11 5 12 15 29 35 56 85
After pass 7 : 3 1 4 8 11 5 12 15 29 35 56 85
After pass 7 : 3 1 4 8 5 11 12 15 29 35 56 85
Pass 8
After pass 12 :1 3 4 8 5 11 12 15 29 35 56 85
ALGORITHM
Algorithm: Let consider Array[ ] is an array, N is size of array
Step 1: start
Sandip Polytechnic,Nashik
13
UNIT II-SERCHING AND SORTING
for(pass=1;pass<=n-1;pass++)
{
for(j=1; j<n-pass;j++)
{
If (A[j]>A[j+1])
{
temp=A[j+1];
A[j+1]=A[j];
A[j]=temp;
}
}
}
printf(“\n Sorted array is:”);
for(i=0;i<n;i++)
{
Printf(“%d”, A[i])
Sandip Polytechnic,Nashik
14
UNIT II-SERCHING AND SORTING
}
}
Disadvantages
1. Bubble sort does not deal well with a list containing a huge number
of items.
This is because the bubble sort requires n-squared processing steps for
every n number of elements to be sorted.
------------------------------------------
Selection Sort:
-Selection Sort algorithm is used to arrange a list of elements in a
particular order (Ascending or Descending).
In selection sort, during pass 1: smallest element from the list get
selected and exchanged with first element in the list.
During pass 2: smallest element from the remaining list get selected
and exchanged with second element from list.
This procedure is repeated till the entire list is sorted.
- List get divided into two parts- sorted list & unsorted list
Sandip Polytechnic,Nashik
16
UNIT II-SERCHING AND SORTING
Algorithm:
Step 1: START
Step 2: Select first element from array or list
Step 3: Compare selected element with all other elements
Step 4: for every comparison if any element is smaller than selected
element then swap two elements.
Step 5: Repeat step 2 to 4 till we get sorted list.
OR
consider Array[], N size
step 1 : START
step 2 : Repeat for i=1 to N-1
step 3 : set Min=Array[i] and loc=i
step 4:repeat for j=i+1 to N-1
step 5: If Min > Array[j]
Then
Set Min =Array [j] and loc=j
Step 6: Interchange Array[i] with Array[loc]
Step 7: STOP
Example 1. : 23 41 11 32 40
Pass 1: i/p: 23 41 11 32 40
o/p: 11 41 23 32 40
Pass 2 : i/p: 11 41 23 32 40
o/p: 11 23 41 32 40
Pass 3: i/P: 11 23 41 32 40
o/p: 11 23 32 41 40
Pass 4: i/p:11 23 32 41 40
o/p: 11 23 32 4041
sorted list is : 11 23 32 40 41
Sandip Polytechnic,Nashik
17
UNIT II-SERCHING AND SORTING
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j ,loc,extra, Array[],temp,n;
printf(“Enter size of array:”);
scanf(“%d”,&n);
for(i=0;i<n-1;i++)
{
temp = Array[i];
loc=I;
for(j=i+1; j<n;j++)
{
If(temp>Array[j])
{
temp = Array[j];
loc=j;
}
}
Extra=Array[i];
Array[i]=Array[loc];
Array[loc]=Extra;
}
for(i=0;i<n;i++)
{
Sandip Polytechnic,Nashik
18
UNIT II-SERCHING AND SORTING
Disadvantage :
--------------------------------
Insertion sort:
- It is another kind of sorting technique because it starts with
index 1 instead of index 0 .
How it works?
Sandip Polytechnic,Nashik
19
UNIT II-SERCHING AND SORTING
EXAMPLE : 23 41 11 32 40
Pass 1: compare 41 with 23
41 is at proper place
So after pass1 array :
23 41 11 32 40
Pass 2 : Compare 11 with 41 & 23
Here 11 is less than 23 so insert 11 before 23
After pass 2: 11 23 41 32 40
Pass 3: Compare 32 with 41 23 11
Insert 32 before 41
After pass 3 : 11 23 32 41 40
Pass 4 : Compare 40 with 41 32 23 11
40 is less than 41 so shift it before 41
After pass 4: 11 23 32 40 41
Example 2:
Elaborate the steps for performing insertion sort for given elements of array.
30 10 40 50 20 45
We take given array:
Iteration 1:
Insertion sort compares the first two elements:
It finds that 30 and 10 are not in sorted order; so it will swap 30 with 10; and the resultant
sorted sub list will be
10 30 40 50 20 45
Iteration 2:
Now it will check for third element; It will first checks 40 with first element that is 10 which is
less than 40; then it will check 40 with 30 and concludes that 30 is lesser than 40 so eventually
it finds that 40 is already in its desirable location so 40 will be added to sub list.
10 30 40 50 20 45
Iteration 3:
In Iteration 3 it will check for fourth element which is 50 which will be compare with 10; it
finds that 10 is smaller than 50, then it will check next element is sub list, which is 30, this new
Sandip Polytechnic,Nashik
20
UNIT II-SERCHING AND SORTING
element is again smaller than 50, so it will consider next element for comparison which is 40.
This element is also smaller so it will keep 50 to its place.
10 30 40 50 20 45
Iteration 4:
Next it will check for fifth element which is 20 with first element which is 10 so it will jump to
next location. At location two it will check with the element 30 with 20 which is greater than
20. So it will shift all elements 30 onwards by one position and inserts 20 at desired location.
10 30 40 50 20 45
10 20 30 40 50 45
Iteration 5:
Now it will check for sixth element which is 45 it is observed as greater than 10 so it will
consider next element for comparison which is 20. After comparison with 20 it will compute
that 20 is smaller than 45 and hence at its appropriate place. Further it will select next
location’s element for comparison which is 30, this element is also smaller than 45 hence next
element will be selected. After comparing next element which is 40 and smaller than 45 last
element will be consider. The last element which is 50 and also greater than 45 will be shifted
by one place and will give list in sorted order.
10 20 30 40 50 45
10 20 30 40 45 50
Disadvantage:
- It best applicable to small list
- Inefficient for Large list
Sandip Polytechnic,Nashik
21
UNIT II-SERCHING AND SORTING
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j , temp ,n, Array[10];
printf(“Enter size of list/array:”);
scanf(“%d”, &n);
printf(“\n Enter Array Elements:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&Array[i]);
}
for(i=1;i<n;i++)
{
temp=Array[i];
j=i-1;
while((temp<Array[j])&& j>=0)
{
Array[j+1]=Array[j];
j=j-1;
}
Array[j+1]=temp;
}
printf(“\n Sorted Array is:”);
for(i=0;i<n;i++)
{
printf(“%d”,Array[i]);
}
}
---------------------------------
4. Quick sort :
Sandip Polytechnic,Nashik
22
UNIT II-SERCHING AND SORTING
Algorithm:
Consider Array[] is list, low is starting index, high is highest index
Quick_sort(Array[],low,high)
{
Pivot=low;
i=low;
j=high;
Sandip Polytechnic,Nashik
23
UNIT II-SERCHING AND SORTING
while(i<j)
while ((array[i]<=pivot )&& (i<high)
i++;
while (Array[j]>pivot)
j--;
if(i<j)
{
temp=array[i];
array[i]=array[j]; swapping of elements at
elements at index I & j
array[j]=temp;
}
temp=array[pivot]; swapping of elements at
elements at pivot& j
array[pivot]=array[j];
array[j]=temp;
}
Quick_sort(array,low,j-1);
Quick_sort(array,j+1,high);
i,j,i = to lowest
J= highest index
I=1 if value at index I is less than pivot
increase value of i .
I=2
I=3 if value at index i is greater than pivot value
Compare pivot with element at index j
Element at jth index may be smaller or greater
If element at j th is smaller than pivot then interchange values at
position i& j
If value at jth index is greater, decrement value of j.
Sandip Polytechnic,Nashik
24
UNIT II-SERCHING AND SORTING
50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j
Compare element at indexi=1 (Array[1]) with pivot
Array[1]<=pivot i.e.30<50
Increase i
50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j
Sandip Polytechnic,Nashik
25
UNIT II-SERCHING AND SORTING
Array[i]=10
Compare Array[i]i.e.Array[2] with pivot (50)
Array[2]<=pivot i.e 10 <50
Increase i
Step 4: Pivot=Array[0], i= 3 , j=7
50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j
Compare Array[3]i.e 90 with pivot (50)
Array[3]>pivot i.e 90 > 50
Stop incrementing i
Step 5 : jump to index j, j=7
50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j
compare Array[j]i.eArray[7] with pivot
Array[7]>pivot i.e. 70>50
Then decrement value of j
Step 6 : Pivot=Array[0], i= 3 , j=6
50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j
Compare Array[j] i.eArray[6]=40 with pivot =50
Array[6]<pivot 40<50
Look at values at index i& j i.e( 90 & 40)
: 50 30 10 40 80 20 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j
Step 7: jump to i
Compare element at ith position with pivot i.e
Array[i] = Array[3]= 40 compare with pivot i.e 50
Array[i] <pivot i..e40<=50 ,
Sandip Polytechnic,Nashik
26
UNIT II-SERCHING AND SORTING
Increase value of i
Step 8 :Pivot=Array[0], i= 4 , j=6
50 30 10 40 80 20 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j
Compare Array[i] i.e. Array[4] with pivot i.e. 50
Array[i] > pivot i.e.80>50
Stop incrementing i
Step 9 :
Jump to j
50 30 10 40 80 20 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j
50 30 10 40 80 20 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j
50 30 10 40 20 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j
Step 11: jump to i
Sandip Polytechnic,Nashik
27
UNIT II-SERCHING AND SORTING
Pivot=Array[0], i= 4 , j=5
50 30 10 4020 8090 70
Index 0 1 2 3 4 5 6 7
Pivot i j
Compare Array[i] i.e. Array[4]=20 with pivot =50
Array[4]<pivot 20<50
Increment i
50 30 10 40 20 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot i/j
Compare Array[i] i.e .Array[5]=80 with pivot
Array[5]>pivot 80>50
Stop incrementing i
Step 10: jump to j
Pivot=Array[0], i= 5, j=5
50 30 10 40 20 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot i/j
Decrement j
: 50 30 10 40 20 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot j i
20 30 10 40 50 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot j i
Sandip Polytechnic,Nashik
28
UNIT II-SERCHING AND SORTING
Sublist 1 sublist 2
elements 20 30 10 40
Index 0 1 2 3
Pivot/i j
i=0 , j=3
pivot=Array[low]=20
Array[i] <=pivot
increment i;
step 12:
pivot =Array[0], i=1,j=3
20 30 10 40
Index 0 1 2 3
Pivot i j
Array[i]>pivot
Stop incrementing i
20 30 10 40
Index 0 1 2 3
Pivot I j
Decrement value of j
Sandip Polytechnic,Nashik
29
UNIT II-SERCHING AND SORTING
20 30 10 40
Index 0 1 2 3
Pivot i j
Array[2]<pivot
Interchange values at index i&j
20 10 30 40
Index 0 1 2 3
pivot i j
pivot/i j
compare pivot with Array[i]=80
increment I
80 90 70
Index 0 1 2
Pivot i j
Compare pivot Array[1]=90
Array[1]> pivot
Stop incrementing i
Comare Array[j] with pivot
Array[2]<pivot
Interchange values at index I & j
80 70 90
Index 0 1 2
Pivot I j
Compare Array[i]=Array[1]=70 with pivot
Array[1]<pivot
Increment i
80 70 90
Index 0 1 2
Pivot i/j
Compare Array[i] with pivot
Array[i]>pivot
Compare Array[j] with pivot
Array[j]>pivot
Decrement j
80 70 90
Index 0 1 2
Pivot j i
70 80 90
Sort the following numbers in ascending order using quick sort. Given
numbers 50, 2, 6, 22, 3, 39, 49, 25, 18, 5
Disadvantage
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j, Array[20],n;
printf(“Enter size of array:”):
Sandip Polytechnic,Nashik
32
UNIT II-SERCHING AND SORTING
scanf(“%d”,&n);
Printf(“%d elements:”,n);
for(i=0;i<n;i++)
{
scanf(“%d”,&Array[i]);
}
Quicksort(Array,0,n-1);
printf(“\n Sorted array is:”);
for(i=0;i<n;i++)
{
printf(“%d”, Array[i]);
}
void Quicksort(Array[20],int low, int high)
{
pivot=low;
i=low;
j=high;
while(i<j)
{
while((array[i]<=pivot) && (i<high))
{
I++;
}
While(array[j]>pivot)
{
j--;
}
If(i<j) array[j]<pivot
{
Temp=array[i];
Array[i]=array[j];
Array[j]=temp;
}
}
temp=pivot;
pivot=array[j];
array[j]=temp;
Quicksort(Array, low, j-1);
---------------------------------
EXAMPLE 2:
Pass 0 1 2 3 4 5 6 7 8 9
Sandip Polytechnic,Nashik
34
UNIT II-SERCHING AND SORTING
1 12 23 44 48
2 12 23 44,48
3 144 248 312 423
Algorithm:
1. Read List of elements
2. Now apply simple procedure to sort elements digit by digit.
2.1 Sort elements according to the last digit (least significant bit)
(unit place) first and place in proper bucket.
2.2 Then sort according to second last digit.
2.3 Then sort according to first/most significant bit and place
element in proper bucket.
3. Print Sorted list.
4. STOP.
OR
Algorithm:
Consider an array A[]
Step 1:start
Step 3: while(largestNum/digitplace>0)
Step5:fori=0 to n do
Count[k]=count[k]+count[k-1]
Do
Result[count[key of A[i]]]=A[j]
Count[key of A[i]] - -
A[i]=result[i]
Digitplace *=10
Disadvantage:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
Int digitplace=1;
Int A[], I,j,k, count[]
Int n;
Sandip Polytechnic,Nashik
36
UNIT II-SERCHING AND SORTING
}
}
--------------------------------------
MERGE SORT
Describe working of merge sort with example.
4M
Ans:
All the elements from the input list are divided into groups. Each group is
sorted
independently and merged together in each iteration. These iterations
continue
performing divide, sort and merge procedure till all elements are placed in
one
single group.
The final group can be sorted with any other sorting method to get a sorted
list.
Example:- input list: 10,1,9,11,46,20,15,0,72,2
Sandip Polytechnic,Nashik
38
UNIT II-SERCHING AND SORTING
In the above example, input list is divided into group of two elements in iteration
1 and sorted in that group. In iteration 2, two groups from iteration 1 are merged
together to form group of four elements. Again each group is sorted and merged
together in iteration 3. In iteration 3, two groups of 4 elements are merged
together to form group of eight elements. Then they are sorted and merge together
to form a group of ten elements in iteration 4. Now all these elements are in a
single group so they are sorted together with any other sorting algorithm.
Sorted list is: 0,1,2,9,10,11,15,20,46,72.
i) Bubble sort
ii) Radix sort
iii) Insertion sort
iv) Selection sort
v) Quick sort
vi) Linear search
vii) Binary search.
Sandip Polytechnic,Nashik
39
UNIT II-SERCHING AND SORTING
**********************************Best Luck***********************************
Sandip Polytechnic,Nashik
40