You are on page 1of 10

UNIT V SORTING and SEARCHING 8

Sorting algorithms: Insertion sort - Quick sort - Merge sort - Searching: Linear search –Binary Search
A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and
lexicographical order.
Bubble Sort
 Bubble sort is simplest sorting algorithm.
 In bubble sort, largest element is moved at highest index in the array. As the largest element moves to the highest index position
it is called that element is “bubbled up” from its position. In the next pass, the second largest element bubbles up, then the third
largest element is bubbles up and so on.
 After the (n-1) passes the list gets sorted. Hence is the name called Bubble sort.
 Bubble sort complexity is O(n2) and only suitable to sort array with small size of data.
Algorithm
1. Read the total number of elements say n.
2. Store the elements in the array.
3. Set i = 0.
4. Compare the adjacent elements.
5. Repeat step 4 for all n elements.
6. Increment the value of i by 1 and repeat step 4,5 for i < n.
7. Print the sorted list of elements.
Implementation
void bubblesort( )
{
int i,j,temp;
for( i=0; i<n-1; i++ )
{
for( j=0; j<n-1-i; j++ )
{
if ( a[ j ] > a[ j+1 ] )
{
temp=a[j];
a[j]=a[ j+1];
a[j+1]=temp;
}
}
} i =0 j 0 1 2 3 4 5 6 7
} 0 5 3 1 9 8 2 4 7 Bubble Sort
1 3 5 1 9 8 2 4 7 Example
2 3 1 5 9 8 2 4 7
Note for 3 3 1 5 9 8 2 4 7 array of size 8, outside i loop repeats 7
times 4 3 1 5 8 9 2 4 7 from 0 to 6.
Bubble 5 3 1 5 8 2 9 4 7 Sort: - (Time Complexity)
- Average Case / Worst Case / Best
6 3 1 5 8 2 4 9 7
i=1 0 3 1 5 8 2 4 7 9 Case: O(n2) 
1 1 3 5 8 2 4 7 Selection sort
2 1 3 5 8 2 4 7
 Selection sort algorithm starts by comparing first two elements of an array and swapping if necessary, i.e., if the first element is
3 1 3 5 8 2 4 7 greater than second then, you need to swap
4 1 3 5 2 8 4 7 the elements but, if the first element is
5 1 3 5 2 4 8 7 smaller than second, leave the elements as
i=2 0 1 3 5 2 4 7 8 it is. Then, again first element and third
1 1 3 5 2 4 7 element are compared and swapped if
necessary. This process goes on until first
2 1 3 5 2 4 7 and last element of an array is compared.
3 1 3 2 5 4 7 This completes the first step of selection
4 1 3 2 4 5 7 sort.
 If there
i = 3are n elements
0 to be 1sorted 3then, the
2 process
4 mentioned
5 7above should be repeated n-1 times to get required result.
Algorithm
1 1 3 2 4 5
1. Stored the unsorted list of elements in an array
2. Then scan the 2array to find the
1 smallest
2 3
element 4 swap
and 5 it with first element,
3. Scan the array3to find the smallest
1 2
element 3and swap
4 it with
5 second element, repeat this process for every element of an array
i = 4array gets
4. Finally 0 sorted. 1 2 3 4 5
5. Print the sorted1 list. 1 2 3 4
2 1 2 3 4 Implementation
void selecton(int a,int n )
i=5 0 1 2 3 4
1 1 2 3 1
i=6 0 1 2 3
1 2
{
int i,j,min,temp;
for(i=0;i<=n-2;i++)
{
min=i;
for(j=i+1;j<=n-1;j++)
{
if(a[j]<a[min])
min=j
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
Example:

Selection Sort: (Time Complexity)


- Average Case / Worst Case / Best Case: O(n2) 

Insertion sort
Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and mostly sorted lists. It works by taking elements
from the list one by one and inserting them in their correct position into a new sorted list. Hence is the name insertion sort.
Algorithm
1. Store the list of unsorted elements in an array.
2. Set a marker for the unsorted zone after the first number in the list.
3. Repeat steps 4 through 6 until the unsorted zone is empty.
4. Select the first unsorted number.
5. Swap this number to the left until it arrives at the correct sorted position.
6. Advance the marker to the right one position
7. Print the sorted list of elements.
Implementaion
void insertionsort( )
{
int i, j, temp;
for (i=1 i<n ; i++ )
{
temp= a[i];
j=i-1;
while( (j>=0) && (a[j] >temp) )
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
} Example : 12 3 1 5 8

2
i=1

i=2

i=3

i=4

Sorted

Explanation: Suppose, you want to sort elements in ascending as in above figure. Then,
1. The second element of an array is compared with the elements that appears before it (only first element in this case). If the
second element is smaller than first element, second element is inserted in the position of first element. After first step, first two
elements of an array will be sorted.
2. The third element of an array is compared with the elements that appears before it (first and second element). If third element is
smaller than first element, it is inserted in the position of first element. If third element is larger than first element but, smaller
than second element, it is inserted in the position of second element. If third element is larger than both the elements, it is kept in
the position as it is. After second step, first three elements of an array will be sorted.
3. Similarly, the fourth element of an array is compared with the elements that appears before it (first, second and third element)
and the same procedure is applied and that element is inserted in the proper position. After third step, first four elements of an
array will be sorted.
If there are n elements to be sorted. Then, this procedure is repeated n-1 times to get sorted list of array.
Insertion Sort:
- Average Case / Worst Case : O(n2) ;
- Best Case : O(n) ; when input is already sorted

Quick Sort
 Quick sort is a divide and conquer sorting algorithm which relies on a partition operation: to partition an array, an element called
a pivot is selected. All elements smaller than the pivot are moved before it and all greater elements are moved after it. The lesser
and greater sub lists are then recursively sorted.
 the algorithm works in Sorting algorithm O(n log n)
Algorithm
1. Choosing the pivot (middle element). The three choice: take the first, the last and the middle element
2. We want larger elements to go to the right and smaller elements to go to the left. Two "fingers" are used to scan the elements
from left to right and from right to left:
 While i is to the left of j, we move i right, skipping all the elements less than the pivot. If an element is found greater then
the pivot, i stops
 While j is to the right of i, we move j left, skipping all the elements greater than the pivot. If an element is found less then
the pivot, j stops
 When both i and j have stopped, the elements are swapped.
 When i and j have crossed, no swap is performed, scanning stops, and the element pointed to by i is swapped with the pivot
 Restore the pivot.
3. Recursively quicksort the left and the right parts
Implementation
void quicksort(int arr[], int low, int high)
{
int i = low, j = high;
int y = 0;

3
int z = arr[(low + high) / 2];
do
{
while(arr[i] < z) i++;
while(arr[j] > z) j--;
if(i <= j)
{
y = arr[i];
arr[i] = arr[j];
arr[j] = y;
i++;
j--;
}
}while(i <= j);
if(low < j)
quicksort(arr, low, j);
if(i < high)
quicksort(arr, i, high);
}
Example
Consider the following unsorted 7 element array. Postion: 0 1 2 3 4 5 6
Elements: 25 3 58 56 7 99 10
1. The first time that the quick sort would be called, low = 0 and high = 6.
2. We calculate the pivot value=> (low+high)/2 value = (0+6)/2 = 3, our pivot value is in at position 3. Element is 56.
3. In the first iteration, i will be low and j will be high, ie. i=0 and j=6
4. Compare arr[i] with the pivot value, 25 is < 56 so increment value of i from 0 to 1
56 0 1 2 3 4 5 6
25 3 58 56 7 99 10
pivot i
j of i to 1
4.a Now i will be 1 and Compare arr[i] with the pivot value, 3 is < 56 so increment value
0 1 2 3 4 5 6
25 3 58 56 7 99 10
i j
4. b Now i will be 2 and Compare arr[i] with the pivot value, 58 is < 56 , so not. stop the i value.
0 1 2 3 4 5 6
25 3 58 56 7 99 10

i j
5. Compare arr[j] with the pivot value, 10 is > 56 so not. stop the j value
0 1 2 3 4 5 6
25 3 58 56 7 99 10
i j
6. Swap arr[i] with arr[j], swap the elements 58 and 10, after swapping, the elements are
0 1 2 3 4 5 6
25 3 10 56 7 99 58

i j
7. After swapping, Increment the i value and decrement the j value
0 1 2 3 4 5 6
25 3 10 56 7 99 58
i j
8. Compare arr[i] with the pivot value, 56 is < 56, so not. stop the i value. Now i will be at position 3.
9. Compare arr[j] with the pivot value, 99 is > 56. so decrement value of j. now j=4.
0 1 2 3 4 5 6
25 3 10 56 7 99 58
i j
10. Compare arr[j] with the pivot value, 7 is > 56. so not, stop the j value. Now j will be at position 4.
0 1 2 3 4 5 6
25 3 10 56 7 99 58
i j
4
11. Swap arr[i] with arr[j], swap the elements 56 and 7, after swapping, the elements are
0 1 2 3 4 5 6
25 3 10 7 56 99 58

i j
12. After swapping, Increment the i value and decrement the j value, the value of i is 4 and j is 3.
0 1 2 3 4 5 6
25 3 10 7 56 99 58

j i
13. Compare i and j, i>j. the first iteration is topped
0 1 2 3 4 5 6
25 3 10 7 56 99 58
j i
Now, the pivot element 56 is sorted at the proper position at 4.
14. Similarly, Two sublists can be sorted independently in recursive order. quicksort(arr, low, j) and quicksort(arr, i, high)
0 1 2 3 4 5 6
25 3 10 7 56 99 58

low i
j high
15. The left sublist would be called, low = 0 and high = 3.
pivot value=> (low+high)/2 value = (0+3)/2 = 1, Element is 3.
Here, i will be low and j will be high, ie. i=0 and j=3 15. a. the right sublist called, low-5 and high=6.
3 pivot value = (5+6)/2 = 5, , Element is 99.
0 1 2 3 Here, i will be low and j will be high, ie. i=5 & j=6
25 3 10 7
pivot i j 99 5 6
16. Compare arr[i] with the pivot value, 25 is < 3, so not. 99 58
stop the i value. i=0.
pivot high
0 1 2 3 i
25 3 10 7 16. a. Compare arr[i] with pivot value, 99 is < 99,
So not. Stop the i value. i=5.
17.a. Compare arr[j] with pivot value, 58 is < 99
17. Compare arr[j] with the pivot value, 7 is > 3 So not. Stop the j value. j=6.
so decrement value of j. now j=2. 18. a. Swap arr[i] with arr[j], swap the elements 25
0 1 2 3 and 3, after swapping, the elements are
25 3 10 7
i j

18. Compare arr[j] with the pivot value, 10 is > 3


19. so decrement value of j. now j=1.
0 1 2 3 5 6
25 3 10 7 58 99
i j
i j
20. Compare arr[j] with the pivot value, 3 is > 3 so not. Stop the j value. 19.a. Increment the i value and decrement
the j value. now i=1 and j=0
0 1 2 3 5 6
25 3 10 7
i 58 99
j
21. Swap arr[i] with arr[j], swap the elements 25 and 3, the elements are
j i
0 1 2 3
3 25 10 7 20.a. Compare i and j, i>j. the iteration is stopped

i j 5 6
58 99
22. Increment the i value and decrement the j value. now i=1 and j=0
0 1 2 3 j i
3 25 10 7 21.a. pivot element 99 is sorted at proper position at 6

j i
25. Compare i and j, i>j. the iteration is stopped 22.a. The left array having one element that also sorted.
0 1 2 3
3 25 10 7
5
5
j i 58
Now, the pivot element 3 is sorted at the proper position at 0.
23. Similarly, the next sub lists can be sorted independently in recursive order. 23.a the sorted right sublists are
1 2 3
5 6
25 10 7
58 99
i j
24. Now, low = 1 and high = 3.pivot value=> (low+high)/2 value = (1+3)/2 = 2, Element is 10.
Here, i will be low and j will be high, ie. i=0 and j=2
10 1 2 3
25 10 7
pivot i j

25. Compare arr[i] with the pivot value, 25 is < 10, so not. stop the i value. i=1.
26. Compare arr[j] with the pivot value, 7 is >10, so not. stop the j value. j=3.
1 2 3
25 10 7
i j

27. Swap arr[i] with arr[j], swap the elements 25 and 3, after swapping, the elements are
1 2 3
7 10 25
i j

28. Increment the i value and decrement the j value. now i and j are 1.
1 2 3
7 10 25

j i
29. Compare arr[i] with the pivot value, 10 is < 10, so not. stop the i value. i=1.
30. Compare arr[j] with the pivot value, 10 is >10, so not. stop the j value. j=3.
31. Swap arr[i] with arr[j], swap the elements 25 and 3, after swapping, the elements are
1 2 3
7 10 25

j i
32. Increment the i value and decrement the j value. now i =3 and j=1.
1 2 3
7 10 25

j i
33. Compare i and j, i>j. the iteration is stopped
1 2 3
7 10 25

j i
Now, the pivot element 10 is sorted at the proper position at 2.
34. The left and right array are having one element that also sorted.
1 2 3
7 10 25
35. the sorted left sublists are the sorted left sublists are
0 1 2 3 4 5 6
3 7 10 25 56 58 99

36. The sorted elements are

6
0 1 2 3 4 5 6
3 7 10 25 56 58 99
Quick Sort:
- Average Case / Worst Case : O(n2) ; happens input is already sorted
- Best Case : O(n log n) ; when pivot divides array in exactly half

Merge Sort
The mergesort algorithm is based on the divide-and-conquer strategy. It operates as follows:
DIVIDE: Partition the n-element sequence to be sorted into two subsequences of n/2 elements each.
CONQUER: Sort the two subsequences recursively using the mergesort.
COMBINE: Merge the two sorted sorted subsequences of size n/2 each to produce the sorted sequence consisting of n elements.
Explanation
Divide
 Here, we divide the given array to be sorted into two halves, sort these two sub-arrays separately, and then combine (merge)
these sorted sub-arrays to produce solution to the original problem.
 For sorting the sub-arrays, we recursively call the mergesort( ) function, to divide the array.
 In recursion technique, we require a stopping point for the recursion. In this mergesort( )function, the situation is detected by the
condition if(low<high) ; when low becomes equal to high meaning that there is only one element in the array, this condition
becomes false. Then, the function returns and the call control starts moving backwards.
Sorting when Combine
Assume, that both arrays are sorted in ascending order and we want resulting array to maintain the same order. Algorithm to
combine two arrays A[0..mid] and A[mid+1...n-1] into an array temp[0….n-1] is as following:
1. Introduce read-indices i, j to traverse arrays A[0..mid] and A[mid+1...n-1], accordingly. Introduce write-index k to store position
of the first free cell in the resulting array temp [0….n-1]. By default i = j = k = 0.
2. At each step: if both indices are in range (i <= mid and j <=high), choose minimum of (A[i], A[j]) and write it to temp[k].
Otherwise go to step 4.
3. Increase k and index of the array, algorithm located minimal value at, by one. Repeat step 2.
4. Copy the rest values from the array, which index is still in range, to the resulting array.
Example

Implementaion
void mergesort(int low,int high){
    int mid;
    if(low<high)
{
         mid=(low+high)/2;
         mergesort (low,mid);
         mergesort (mid+1,high);
         combine(low,mid,high);
    }
}
void combine (int low,int mid,int high)
{
int i,j,k,l,temp[MAX];
    k=low;
    i=low;
    j=mid+1;
while((i<=mid)&&(j<=high))
7
{

         if(a[i]<=a[j])
{
             temp[k]=a[i];
             i++;
k++;
         }
         else
{
             temp[k]=a[j];
             j++;
k++;
         }
      }
   while((i<=mid)
{
             temp[k]=a[i];
             i++;
k++;
     }
   while((j<=high)
{
             temp[k]=a[j];
             j++;
k++;
     }
    for(k=low;k<=high;k++)
         a[k]=temp[k];
}
Merge Sort :
- Average Case / Worst Case / Best case : O(n log n) ; doesn't matter at all whether the input is sorted or not

Searching
The process of finding the location of an element with a specific value (key) within a collection of elements is called searching.
Basic Searching techniques: Two Types
1. Sequential search or Linear Search
• sequential search usually is implemented to search item from unsorted list/ array.
•The technique can be implemented on a small size of list. This is because the efficiency of sequential search is low compared to other
searching techniques.
• In a sequential search:
1.Every element in the array will be examine sequentially, starting from the first element.
2.The process will be repeated until the last element of the array or until the searched data is found.
• Search key is compared with all elements in the list, O(n) time consuming for large datasets.
Algorithm
1.Examines each element in the array one by one (sequentially) and compares its value with the search key
2.Search is successful if the search key matches with the value being compared in the array. Searching process is terminated.
3.else, if no matches is found, the search process is continued to the last element of the array. Search is failed array if there is no matches
found from the array.
Implementation
void sequentialsearch(int key)
{
int flag=0,mark;
for(int i=0;i<n;i++) //searching an element from beginning of an array
{
if(a[i]= =key)
{
flag=1; //setting the flag if element is found
mark=i; //marking the location of key element
break;
}
}
if(flag= =1) //flag=1 means element is found
cout<<”\n The element is present at location:”<<mark+1;
else
cout<<”\n The element is not present in the array”;
8
}
Example
key 22 flag=0 (false)
[0] [1] [2] [3] [4]
array 11 33 22 55 44
i=0
[0] [1] [2] [3] [4]
array 11 33 22 55 44
a[0]= =key
ie. 11= = 22 no
flag=0 (false)
i=1
[0] [1] [2] [3] [4]
array 11 33 22 55 44
a[1]= =key
ie. 33= = 22 no
flag=0 (false)
i=2
[0] [1] [2] [3] [4]
Array 11 33 22 55 44
a[2]= =key
ie. 22= = 22 yes
flag=1 (true)
Search for key 22 is successful & return 2
Linear Search:
- Worst Case and Average Case : O(n) ; search key not present or last element
- Best Case : O(1) ; first element

2. Binary Search
- Consider a list in ascending sorted order.
- For a sequential search, searching is from the beginning until an item is found or the end is reached. Binary search improve the
algorithm by removing as much of the data set as possible so that the item is found more quickly.
- Search process is started at the middle of the list, then the algorithm determine which half the item is in the list.
- It divides the working range in half with a single test. By repeating the procedure, the result is an efficient search algorithm-
O(log2 n).
Algorithm
It starts with the middle element of the list.
1. If the middle element of the list is equal to the ‘search key’ then we have found the position the specified value.
2. Else if the ‘search key’ is lesser than the middle element then the ‘search key’ has to be present in the first half of the list.
3. Or if the ‘search key’ is greater than the middle element then the ‘search key’ has to be present in the last half of the list.
4. Search is repeated until the searched key is found or the low>high
Hence, the search list gets reduced by half after each iteration.
Implementation
int binarysearch (int low, int high, int x)
{
if(low > high) // if the array exhausted
return -1; //not found return -1
int mid = (low + high)/2; // if not, find middle index
if(x= =a[mid]) //if the key is here or not
return mid; //if so, return index
else if(x < a[mid]) //else if key is smaller
binarysearch(low, mid-1, x); //reduce the high end, compare with first half
else // otherwise, key is greater
binarysearch(mid+1, high, x); //increase low end, compare second half
}
Example:
11 22 33 44 55 66
search_key , x= 55, n = 6 (number of elements)

[0] [1] [2] [3] [4] [5]


11 22 33 44 55 66
Low high
step1 :
low=0
high=5
low>high i.e 0>5
9
mid= 0+5/2 =2  mid=2 { First MIDDLE index}
[0] [1] [2] [3] [4] [5]
11 22 33 44 55 66
Low mid high
if search_key ==a[2]
ie. 55= =33 no
else if x < a[2]
i.e 55 <20 no
else x > a[2]
i.e 55 >20 yes
so search key has to be present in the last half of the list
low=mid+1 ie. low = 2+1  3

step 2:
low=3
high=5
low>high i.e 3>5
mid= 3+5/2 =4  mid=4{ Second MIDDLE index}
[0] [1] [2] [3] [4] [5]
11 22 33 44 55 66
Low mid high
if x ==a[4]
ie. 55= =55 yes so return the mid element ie. 4
search element x=55 is at the location 4 (mid=4)
Binary Search:
- Worst case/Average case : O(n log n) 
- Best Case : O(1) ; when key is middle element

10

You might also like