Searching Algorithms
Searching is a process of finding a particular record, which can be a single element or a small
chunk, within a huge amount of data. The data can be in various forms: arrays, linked lists, trees,
heaps, and graphs etc. With the increasing amount of data nowadays, there are multiple
techniques to perform the searching operation.
Searching Algorithms:
Various searching techniques can be applied on the data structures to retrieve certain data. A
search operation is said to be successful only if it returns the desired element or data; otherwise,
the searching method is unsuccessful.
Sequential Searching
As the name suggests, the sequential searching operation traverses through each element of the
data sequentially to look for the desired data. The data need not be in a sorted manner for this
type of search.
Example − Linear Search
Linear search is a type of sequential searching algorithm. In this method, every element within
the input array is traversed and compared with the key element to be found. If a match is found
in the array the search is said to be successful; if there is no match found the search is said to be
unsuccessful and gives the worst-case time complexity.
Linear Search Algorithm
The algorithm for linear search is relatively simple. The procedure starts at the very first index of
the input array to be searched.
Step 1 − Start from the 0th index of the input array, compare the key value with the value present
in the 0th index.
Step 2 − If the value matches with the key, return the position at which the value was found.
Step 3 − If the value does not match with the key, compare the next element in the array.
Step 4 − Repeat Step 3 until there is a match found. Return the position at which the match was
found.
Step 5 − If it is an unsuccessful search, print that the element is not present in the array and exit
the program.
C Program to Search an Array Element using LINEAR SEARCH
#include <stdio.h>
int main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
return 0;
OUTPUT:
Enter the number of elements in array
5
Enter 5 numbers
12
23
22
10
45
Enter the number to search
22
22 is present at location 3.
Binary Search:
Binary Search is an interval searching algorithm that searches for an item in the sorted list. It
works by repeatedly dividing the list into two equal parts and then searching for the item in the
part where it can possibly exist.
Rules to Apply Binary Search
Unlike linear search, there are a few conditions for applying binary search:
1. The list must be sorted.
2. Random access to the list members.
It means that we cannot use the binary search with unsorted or linked data structures.
Algorithm for Binary Search in C
Let key be the element we are searching for, and the array be sorted in the ascending order.
1. Compare key with the middle element of the array.
2. If key matches with the middle element, we return the index of the middle element.
3. Else if key is greater than the middle element, it means that key can only lie in the right
half sub array after the middle element. So we repeat steps 1 to 4 for the right half sub
array and leave the left half sub array.
4. Else if key is smaller than the middle element, it means that target can only lie in the left
half sub array before the middle element. So, we repeat steps 1 to 4 for the left half sub
array.
5. We will keep doing that till we find key or there is no element left in the sub array being
considered.
#include<stdio.h>
#include<conio.h>
int main()
{
int i, arr[10], search, first, last, middle;
printf("Enter 10 elements (in ascending order): ");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
printf("\nEnter element to be search: ");
scanf("%d", &search);
first = 0;
last = 9;
middle = (first+last)/2;
while(first <= last)
{
if(arr[middle]<search)
first = middle+1;
else if(arr[middle]==search)
{
printf("\nThe number, %d found at Position %d", search, middle+1);
break;
}
else
last = middle-1;
middle = (first+last)/2;
}
if(first>last)
printf("\nThe number, %d is not found in given Array", search);
getch();
return 0;
}
-------------------------------------------------------------------------------------------------------------