8 Searching and Sorting
8 Searching and Sorting
Chapter 8
Searching and sorting
Narasimhan T.
8.1 Searching
Given an array A and a value called key, the problem of searching is to determine if the key
is present in A or not. If the key is present, the positions are also printed. Two prominent
approaches to searching are linear search and binary search whose discussions are in order
now.
Example 8.1. Let the array A be ⟨5, 0, 3, -2, 7, 6, 3⟩ and key be 3. The working
of linear search algorithm on this input is shown in Table 8.1. The final output is 2 6. ■
#include<stdio.h>
main()
{
int a[10],i,n,key,flag=0;
printf("Enter the array size\n");
scanf("%d",&n);
119
120 CHAPTER 8. SEARCHING AND SORTING
#include<stdio.h>
main()
{
int a[10],i,n,key,low,high,mid,flag=0;
printf("Enter the array size\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
122 CHAPTER 8. SEARCHING AND SORTING
{
mid=(low+high)/2;
if(a[mid]==key)
{
flag=1;
printf("Key found at index %d\n",mid);
break;
}
else if(key>a[mid])
low=mid+1; //Search in right half
else
high=mid-1; // Search in left half
}
if(flag==0)
printf("Sorry, the key %d is not present in the array\n",key);
}
8.2 Sorting
Sorting means arranging the elements of an array in an order. The order could be ascending
or descending. Unless otherwise specified, ascending order is the norm. There are a good
number of sorting algorithms. The most common one is the bubble sort which is discussed
here.
Example 8.3. Suppose you want to sort the numbers ⟨5, 1, 4, 7, 2, 6⟩. You consider the
elements in pair and swap them if necessary. The various passes are shown in Figure 8.1.
The pair of elements which are under current consideration are underlined. In pass 0, the
largest element 7 moves to its correct position. This is indicated by showing 7 in a box.
In pass 1, the next largest element 6 settles down. Thus, now 6 and 7 are in their correct
positions shown by putting them in a box. This is continued until you end up with a single
element, which is by default sorted. Finally the sorted list is output. ■
8.2. SORTING 123
Input
5 1 4 7 2 6
Pass 0
5 1 4 7 2 6 (swap)
1 5 4 7 2 6 (swap)
1 4 5 7 2 6
1 4 5 7 2 6 (swap)
1 4 5 2 7 6 (swap)
1 4 5 2 6 7
Pass 1
1 4 5 2 6 7
1 4 5 2 6 7
1 4 5 2 6 7 (swap)
1 4 2 5 6 7
1 4 2 5 6 7
Pass 2
1 4 2 5 6 7
1 4 2 5 6 7 (swap)
1 2 4 5 6 7
1 2 4 5 6 7
Pass 3
1 2 4 5 6 7
1 2 4 5 6 7
1 2 4 5 6 7
Pass 4
1 2 4 5 6 7
1 2 4 5 6 7
Output
1 2 4 5 6 7
• In pass 0, n − 1 elements are compared with their adjacent elements in pair (Note
that the last element has no adjacent neighbour) and swapped if necessary. In pass
1, you compare n − 2 elements with their neighbours. Thus in pass i, you need to
consider only n − (i + 1) or n − i − 1 elements.
#include<stdio.h>
main()
{
int a[10],i,j,n,temp;
printf("Array size please\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
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;
}
}
}
printf("Array elements in ascending order:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
• While considering strings pairwise, the swapping is to be done only when the second
string of the pair alphabetically precedes the first. This check is to be accomplished
using strcmp() function.
• The temp variable used in the swapping process should also be a string.
8.2. SORTING 125
• Since one string cannot be assigned to another, strcpy() is to be used while swapping.
#include<stdio.h>
#include<string.h>
main()
{
char strs[10][10],temp[10];
int i,j,n;
printf("How many strings do you want to sort?\n");
scanf("%d",&n);
printf("Enter the strings\n");
for(i=0;i<n;i++)
scanf("%s",strs[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(strcmp(strs[j],strs[j+1])>0)
{
strcpy(temp,strs[j]);
strcpy(strs[j],strs[j+1]);
strcpy(strs[j+1],temp);
}
}
}
printf("Strings in ascending order:\n");
for(i=0;i<n;i++)
printf("%s\n",strs[i]);