Searching is required for extracting a specific data from a data structure. Binary Search is an efficient technique of searching data from sorted structures. It is very much like searching a particular page in a book.
Let us consider a book having 100 pages. We need to search, say, page 36. We proceed as follows:
Let us consider a book having 100 pages. We need to search, say, page 36. We proceed as follows:
- We divide the book in half and open the middle page. Here it is page 50. We see that our search page has a lower value than this page, so we discard the pages from 51 to 100 and take the first half in consideration.
- We open the middle page of this portion (page 1 - page 50). Here it is page 25. We see that our search page has a higher value than this page, so we discard the pages from 1 to 25 and take the second half in consideration.
- We open the middle page of this portion (page 26 - page 50). Here it is page 38. We see that our search page has a lower value than this page, so we discard the pages from 39 to 50 and take the first half in consideration.
- We open the middle page of this portion (page 26 - page 38). Here it is page 33. We see that our search page has a higher value than this page, so we discard the pages from 26 to 33 and take the second half in consideration.
- We open the middle page of this portion (page 34 - page 38). Here it is page 36. This is our search page and we have found it.
This same method is applied to a sorted array to find a particular entry in the array. A graphical representation of Binary Search is given below.
Fact:
Worst-case performance is O(log n) and best-case performance is O(1), where n is the total number of elements in the array.
Now, the Binary Search algorithm can be implemented either by using an iterative procedure or by a recursive procedure. Since the recursive procedure is better and more efficient among the two, I am providing the source code of the recursive procedure only.
Note: It is assumed that the user will enter the data in ascending order of values; otherwise the array will have to be explicitly sorted in the program.
Program Listing:
#include <stdio.h>
#include <conio.h>
int arr[50],num; // Global declaration of the array and number to be searched
void BSearch(int low,int high)
{
int mid=(high+low)/2;
if(low>=high) // Breaking condition
{
printf("\n%d is not present in array",num);
return;
}
else if(arr[mid]==num) // Breaking condition
{
printf("\n%d is present in array",num);
return;
}
else if(arr[mid]<num)
BSearch(mid+1,high);
else if(arr[mid]>num)
BSearch(low,mid-1);
}
void main()
{
int n,i;
printf("How many numbers will you enter? (<=50): ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter #%d: ",i+1);
scanf("%d",&arr[i]);
}
printf("\nEnter number to search: ");
scanf("%d",&num);
BSearch(0,n-1); // Call to the Binary Search function
getch();
}
Output:
How many numbers will you enter? (<=50): 5
Enter #1: 12
Enter #2: 45
Enter #3: 56
Enter #4: 78
Enter #5: 92
Enter number to search: 78
78 is present in array