Sorting & Searching
Sorting & Searching
Unit - 4
Sorting:-
Process for arranging the all or some item in a set of item in a particular
order or sequence is called sorting.
Sorting Algorithm:-
1. Bubble sort :-
In this method the comparison of each element in the list with entire
item and swapping them if require.
While the insertion, shell sort and selection sort also have O (n2)
complexity they are more efficient then the bubble sort.
Pass 1:-
first of all the first element will compare with second and first >
second then swap otherwise do nothing. For our example we found 5 is
greater then 1 so we get 1 5 4 0 and initially value of exch
=0 and now become 1 incremented by one.
Then compare the second element with third and if second > third then
again swap otherwise remain as it is. In our case we found the result like
Pass 2:-
Second time comparison made for second and third and we get the
result like 1 0 4 5 because the second element > third. And
value of exch = 1.
Pass 3:-
again compare from first to end element and for first trace we found
first > second so we get 0 1 4 5
Then for second and third trace we don’t require to swap means the
condition is not satisfy so all the element are in proper position so finally
we get the array in the form of 0 1 4 5
1. [initialize]
last = N
2. [repeat through step 7 , n-1 time ]
For pass = 1 to n-1 do
3. [initially exchange none]
Exch = 0
4. [repeat up to 5 for unsorted ]
For I =1 to last do
5. [compare element]
If ( a[ I ] > a[ I +1]
a[i] = a [ I +1]
exch = exch +1
6. [ check for sorting complete]
If exch = 0 then
Return
7. [change last unsorted position]
Last = last -1
void main()
{
int i,last,n,pass,exchs,k[25];
clrscr();
printf("\nEnter the size of array N:");
scanf("%d",&n);
printf("\nEnter Unsorted List:");
for(i=1;i<=n;i++)
{
printf("\nEnter [%d]the Element",i);
scanf("%d",&k[i]);
}
last=n;
for(pass=1;pass<=n-1;pass++)
{
exchs=0;
for(i=1;i<=last-1;i++)
{
if(k[i]>k[i+1])
{
int t;
t=k[i];
k[i]=k[i+1];
k[i+1]=t;
exchs=exchs+1;
}
}
if(exchs==0)
{
break;
}
else
{
last=last-1;
}
}
printf("The Sorted List Is :");
for(i=1;i<=n;i++)
{
printf("\n%d",k[i]);
}
getch();
}
Output :-
2. Selection Sort:-
Pass 1:-
Pass2 :-
At the end of first pass we found the first element are placed at
proper position so it will start from second element and say min_index to
the second element and then compare it with all other element whichever is
finally smallest we found it will become the final min_index and swapped
with the second element of array so the second element also found at
proper position.
Pass 3:-
5 min-index 0 0 0
1 1min_ index 1 1
4 4 4 min_index 4
0 5 5 5
Selection( k,n)
K an array
N Number of element
1. [repeat through step 5 , n-1 time]
For pass =1 to n-1
6. [finished]
Return.
#include<stdio.h>
#include<conio.h>
void sel_sort();
int i,k[25],n,pass,minindex;
void main()
{
clrscr();
sel_sort();
getch();
}
void sel_sort()
{
printf("\nEnter N:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter [%d]th element",i);
scanf("%d",&k[i]);
}
for(pass=1;pass<=n-1;pass++)
{
minindex=pass;
for(i=pass+1;i<=n;i++)
{
Prepared By Prof. B.N.Patel 6
Data Structure Management
if(k[i]<k[minindex])
{
minindex=i;
}
}
if(minindex!=pass)
{
int t;
t=k[pass];
k[pass]=k[minindex];
k[minindex]=t;
}
}
printf("\nThe Sorted List Is:");
for(i=1;i<=n;i++)
{
printf("\n%d",k[i]);
}
}
3. Radix Sort:-
0 1 2 3 4 5 6 7 8 9
12 23 34 15 17 78 09
04 25 67 58 29
In above we can put the element I bucket sequence like the 15 has least
significant digit 5 so put it on 5th bucket for the element 23 has digit 3 so
put it on 3rd bucket and so on.
At the end of the procedure we found the position of all elements with
different bucket and now we arrange the entire element from lower to upper
sequence then e found the array like this
12 23 34 04 15 25 17 67 78 58 09 29
Again do the same procedure and put the element in bucket as per the most
significant digit. Then we get the following result
0 1 2 3 4 5 6 7 8 9
04 12 23 34 58 67 78
09 15 25
17 29
Now again we collect the entire element in order from 0 bucket to 9 bucket
then we found the total array in proper position and whole array in
ascending order.
Final array after second pass
04 09 12 15 17 23 25 29 34 58 67 78
If in above sorting method we have element with more then two digit then
also can be handle with the same procedure but in that case we have to
proceed for number of time equal to the maximum digit in our list.
T[ I ] = b[ I ] = Empty
3. [start from beginning of list]
R = First
Td = r
Link( R) Null (end of packet)
R= next (next ele. Of list)
6. [Finished]
4. Merge Sort:-
Merge sort is to be done with the two separate sorted lists. In that
case we have initially two list of element and all are in sorted form now we
want to place them all to the one list and also with order.
Merge sort is slightly faster then the heap sort but due to use of additional
memory for separate array it becomes unattractive.
In that method first of all we take the first element of both list and compare
the two elements which one is smaller then put it on new final list and the
increment the index of the smallest element list and then again compare the
higher with the new element of other list and so on.
Ex. A1 1 11 23 42 52 83
A2 2 9 25
Here two list A1 and A2 so first start with the first element of both
array means 1 and 2 here I is smaller then 2 so place 1 to final list and
increment the next index means the element 11 is compare with 2 again.
Now in second comparison 2 is smaller so place it in final list and increment
the second list element and next comparison will made with the 11 and 9.
and so on. Finally we found the final array is
A3 1 2 9 11 23 25 42 52 83
1. [initialize]
I First
J Second
K0
5. [finished]
Return
5. Quick Sort :-
Quick sort is very efficient method for larger list the basic principal
is that one time one key value is place to the position and dividing list in
to the two parts where the left part contain the smaller then key value
and right part contain the larger then key value.
Similar process will be continuing until all the elements are placed
in proper position. In each and every stage we are divided the list in two
part and based on that we sort the list so some time we called the
method is partition exchange sort also.
Ex. here one array is given like
15 9 17 23 25 12 34 4 67 29
From this array first value take as key and then decide the second
most element is first index I and last element is j index.
Now increment the index I as long as value of I found less then the
key value. And when we found the oppose result just stop to increment
the I and start with j index. Index j will be decremented as long as value
if j found the greater then the key value.
When both index become stable then check the condition is i<j then
exchange the value of index with I and j respectively.
15 9 17 23 25 12 34 4 67 29
Key I J
15 9 17 23 25 12 34 4 67 29
Key I J
15 9 17 23 25 12 34 4 67 29
Key I J
Here we can see the both index become stable so we have to check
the condition here the value of index I < j so exchange the element of I and j
index. Now our list become.
15 9 4 23 25 12 34 17 67 29
Key I J
15 9 4 23 25 12 34 17 67 29
Key I J
Again I < j so exchange the index value and get array like
15 9 4 12 25 23 34 17 67 29
Key I J
15 9 4 12 25 23 34 17 67 29
Key J I
Now here we can see the j is become less then I so swap the value of
j with key we found the array like
12 9 4 15 25 23 34 17 67 29
And the same procedure done with the individual list and get all
element in proper position and finally we can get the array in sorted form.
6. INSERTIONSORT(L, N)
Prepared By Prof. B.N.Patel 12
Data Structure Management
[Initialize]
L [0]-0
[Loop on Array]
Repeat thru step 5 for I = 1 to N
[Set temporary variable and pointer]
TEMPL [I]
PointerI-1
[Compare elements]
While (TEMP< L [Pointer])
L [Pointer + 1] L [Pointer]
Pointer Pointer -1
[Insert element to its proper place]
L [Pointer] TEMP
[Finished]
Searching:-
For finding the element from the given list is call the searching and
for that there are two methods are available here.
1. Sequential search :-
Sequential search is also called the linear search. Means we
compare all the elements one by one and try to match the targeted
element. If we found the element successfully then display it otherwise
give the result there is no such element.
78 16 20 15 97 47 50 60
From that now we want to search the element 15 from the list
then what we have to do. first of all we compare the first element of list if
it is 15 or not, in our are the first is 78 so go to next the second is 16 so
continue until the 15 come if before reach at last element if we found the
15 then display it with position otherwise display the message there is no
Drawback:-
For searching the element we have to search the whole list if our
targeted element are located at last. It will take more time to search.
2. Binary search:-
This is also one searching method but it will work only with sorted
array.
The basic concept of the method is very simple when we want to find
any element from list then we can eliminate the half list directly by checking
only one element.
Ex. supposes we have a list of 500 digits from 1 t o1000 and we want to find
out 93 from the list.
First of all we divided the list in to part from middle our meddle
element may be 500 so obviously we have to search only from the one half
and which is 1 to 500 only forget about the second half 501 to 1000.
Again the new list is divided with two part so our targeted element is located
in first 1 to 150 element so again forget about the second half 251 to 500.
Similarly we can find the targeted element very quickly and no need to
check the entire element from list.
Algorithm for Binary search:-
1. [initialize]
Low= 1
High = n-1
2. [ repeat through 6]
While (low <=high)