0% found this document useful (0 votes)
16 views

Sorting & Searching

The document discusses different sorting algorithms: 1. Bubble sort is the simplest sorting algorithm but also the slowest. It has a time complexity of O(n^2). 2. Selection sort improves upon bubble sort with a time complexity still of O(n^2). It finds the minimum element and swaps it into the sorted portion of the array. 3. Radix sort is useful for sorting numbers and has a time complexity of O(nk) where k is the number of digits. It distributes elements into buckets based on individual digits then reassembles them.

Uploaded by

AsHu Xd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Sorting & Searching

The document discusses different sorting algorithms: 1. Bubble sort is the simplest sorting algorithm but also the slowest. It has a time complexity of O(n^2). 2. Selection sort improves upon bubble sort with a time complexity still of O(n^2). It finds the minimum element and swaps it into the sorted portion of the array. 3. Radix sort is useful for sorting numbers and has a time complexity of O(nk) where k is the number of digits. It distributes elements into buckets based on individual digits then reassembles them.

Uploaded by

AsHu Xd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structure Management

Data Structure Management


Sub Code:- 330701

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.

Classification:- sorting algorithm are classified by following

1. Compiler complexity: - worst average and best behavior of element


comparison in term of size of list (n). For typical sorting algorithm
good behavior is (n log n) and in bad behavior id Ω (n2) and for
ideal behavior for sort is O (n).

2. Memory Usage:- Use of other compiler resources , some sorting


algorithm are “ in place” such that only O (1) or O ( log n)
memory needed for item sorting while other need to create
auxiliary location for data to be temporarily storage.

3. Recursion: - Some algo are either recursive or non recursive while


other may be both.

4. Stability:- Maintain the relative order of records with equal key.

 Sorting Algorithm:-

1. Bubble sort :-

Bubble sort is the oldest and simplest method and unfortunately it


is also a slowest method.

In this method the comparison of each element in the list with entire
item and swapping them if require.

This process will remain continue until we get pass without


swapping.

Bubble sort generally consider being the most inefficient sorting


algorithm and its complexity is O (n).

Prepared By Prof. B.N.Patel 1


Data Structure Management

While the insertion, shell sort and selection sort also have O (n2)
complexity they are more efficient then the bubble sort.

Ex. here one array with element 5 1 4 0

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

1 4 5 0 and value of exch = 2

Continue with same we found for third and fourth digit 1 4 0 5


Here we found the value of exch > 0 so go for second pass.

Pass 2:-

again do the same procedure from start means first is compare


with second and if first > second then swap otherwise nothing so we get.
1 4 0 5 remain as it is.

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.

Similarly we found in third trace and result will be 1 0 4 5

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

Here in last pass there is no change the value of exch. It is remain 0


so it is the last sequence of our array and finally we get the array in the
ascending order means in sorting form.
Prepared By Prof. B.N.Patel 2
Data Structure Management

Algorithm for bubble sort (a,n)

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

Implementation of bubble sort method with prog. in c.


#include<stdio.h>
#include<process.h>
#include<conio.h>

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++)

Prepared By Prof. B.N.Patel 3


Data Structure Management

{
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 :-

Enter the size of array N


5
Enter Unsorted list
55
36
25
85
94

The sorted list is


25
36
55
85
94

Prepared By Prof. B.N.Patel 4


Data Structure Management

2. Selection Sort:-

Pass 1:-

In selection sort method randomly we take first element as


min_index then compare the element with all other element and if during
the comparison if we found the any element is less then the first min_index
then made the new min_index to that smallest element and at the end of
comparison whichever the min_index will be swapped by the first element.
So finally the smallest element will be placed at the first position
and then we go for second pass.

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:-

At he end of pass 2 we found the two element is already placed at


proper place so start with the third element and do the same procedure for
that element so finally we found the three element in proper way similarly
we can store all the element of array in proper position and we get the array
in final ascending order.
Ex. 5 1 4 0

5 min-index 0 0 0
1 1min_ index 1 1
4 4 4 min_index 4
0 5 5 5

Algorithm of Selection Sort

Selection( k,n)

K an array
N Number of element
1. [repeat through step 5 , n-1 time]
For pass =1 to n-1

Prepared By Prof. B.N.Patel 5


Data Structure Management

2. [select the current position as min-index]


Min_index= Pass

3. [repeat through 4 on unsorted order]


For i= pass+1 to n-1 do

4. [compare elemet with current min_index]


if ( k[min_index] > k[i]) then
min_index= I

5. [check new minimum is found]


If(min_index != pass) then
K[min_index k[Pass]

6. [finished]
Return.

 Implementation the selection sort using c language.

#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:-

Radix sort is the sorting method which is very useful specifically


when data value in list is very smaller then the list containing decimal no.
In this method there are 10 different buckets and all the buckets contain
the proper digit from 0 to 9 and from initially we can take the element fro
array and put it in specific bucket as per the least significant digit match.
We can understand by example.

Ex. list we have is


15 23 17 09 25 12 34 04 67 29 78 58

Now the bucket is

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

Prepared By Prof. B.N.Patel 7


Data Structure Management

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.

Algoritm for Radix Sort Method:-

First :- starting add of list


T, B, :- top and Bottom of array
D :- Digit obtain from key
M :- total number of digit in key
Next :- next element in list
P :- represent pocket number

1. [perform sort for each digit]


For j = 1 to n]

2. [ initialize the packet to empty]


For I = 0 to 9]

T[ I ] = b[ I ] = Empty
3. [start from beginning of list]
R = First

4. [distribute each record in proper packets]


While r != Null do
D= digit(Key, J) ( obtain Jth Digit)
Next = link ( R) (save next)
if td – Null then
Td = bd = r
Else
Link(Td) = r
Prepared By Prof. B.N.Patel 8
Data Structure Management

Td = r
Link( R) Null (end of packet)
R= next (next ele. Of list)

5. [Combine the packet]


P =0
While ( b[p] = null
P = p+1 (Move to the first non empty packet]
First = p[b]
For I = p+1 to 9
Prev = t[I – 1]
If (t[i] != null) then
Link (prev) = B[ I ]
Else
T[ I ] = prev

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

Algorithm for Merge sort:-


Prepared By Prof. B.N.Patel 9
Data Structure Management

Temp  temporary table


A  current array to be sorted
First first element index of first array
Second  first element index of second array
Third total number element in array

1. [initialize]
I  First
J Second
K0

2. [compare the corresponding and output smallest]


Repeat while 1 <second && j <third
If ( a[ I ] < a[ j ]) then
K = k+1
Temp [ I ] = a[ I ]
I = i+1
Else
K = k+1
Temp[ j ] = a[ j ]
J = j+1

3. [copy remaining unprocessed element in output area]


If(i> second) then
While( j<=third)
K = k+1
Temp[k] = a[ j ]
J = j+1
Else
While( I < second)
K = k+1
Temp[k] = a[ I ]
I = i+1

4. [copy the element in to the temporary vector in to original area]


Repeat for I = 1 to k
A[first -1 +I ] = temp[ I ]

5. [finished]
Return

Prepared By Prof. B.N.Patel 10


Data Structure Management

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

Index I value 9 is smaller then key so increment I and place on


element 17 again check but now 17 is greater then 15 so stop to increment I
and start with j index.

15 9 17 23 25 12 34 4 67 29

Key I J

Now check the element on index j is 29 is greater then 15 so


decrement j and place it on element 67 again 67 is greater the n 15 so again
decrement and place on 4 but now the 4 is less then key value so stop over
here.

Prepared By Prof. B.N.Patel 11


Data Structure Management

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

Again the same procedure 4 is less then key so increment and


placed on 23 and stop there. Other side the index j is greater then key up to
the value 12 so j will be stop on 12. we get the position of array like

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

Again same procedure the 12 is smaller so I will be on 25 and become


stable. And the j will be pass on from 25 ad 12 and stable on 12.

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

In above array we can easily identify the 15 is come to its proper


position because the all element left from 15 are less then it and remaining
all in right side are greater then 15 so partitions the list in two part from
element 15.

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

L is an array consist of N elements

 [Initialize]
L [0]-0
 [Loop on Array]
Repeat thru step 5 for I = 1 to N
 [Set temporary variable and pointer]
TEMPL [I]
PointerI-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.

Ex. here we have provided one array like

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

Prepared By Prof. B.N.Patel 13


Data Structure Management

element. Fortunately here we found the 15 element at position 4th so


display it.

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)

3. [find middle element]


Mid = (low + high)/2

4. [check for element]


If k [mid] = x then
Return (mid)

5. [check the element from lower part]


If (k[ Mid] > x) then
Low = mid -1

Prepared By Prof. B.N.Patel 14


Data Structure Management

6. [check the element from upper part]


If k[mid] < x then
High = mid -1

7. [element not found]


Return (-1)

 Comparison between sequential search and binary search


Sequential Search Binary Search
(1) Sequential Search is used (1) Binary Search is used to find
to find elements from elements from ordered list.
unordered list
(2) This technique is less (2) This technique is more
efficient efficient
(3) The order of sequential (3) The order of Binary Search
search is O(N). is O(Log2N)

Prepared By Prof. B.N.Patel 15

You might also like