Binary Search in Data Structure

Rumman Ansari   Software Engineer   2023-03-27   6275 Share
☰ Table of Contents

Table of Content:


Binary search algorithm can be applied on a sorted array to search an element. Search begins with comparing middle element of array to target element. If both are equal then position of element is returned. If target element is less than middle element of array then upper half of array is discarded and again search continued by dividing the lower half. If target element is greater than middle element then lower half is discarded and search is continued in upper half.

Binary search

Time Complexity

Worst Case Time Complexity: O(log n)

Best Case Time Complexity: O(1)

C program for binary search

C program for binary search: This code implements binary search in C language. It can only be used for sorted arrays, but it's fast as compared to linear search. If you wish to use binary search on an array which isn't sorted, then you must sort it using some sorting technique say merge sort and then use the binary search algorithm to find the desired element in the list. If the element to be searched is found then its position is printed. The code below assumes that the input numbers are in ascending order.

C programming code for binary search


 #include <stdio.h>
 
int main()
{
   int c, first, last, middle, n, search, array[100];
 
   printf("Enter number of elements\n");
   scanf("%d",&n);
 
   printf("Enter %d integers\n", n);
 
   for (c = 0; c < n; c++)
      scanf("%d",&array[c]);
 
   printf("Enter value to find\n");
   scanf("%d", &search);
 
   first = 0;
   last = n - 1;
   middle = (first+last)/2;
 
   while (first <= last) {
      if (array[middle] < search)
         first = middle + 1;    
      else if (array[middle] == search) {
         printf("%d found at location %d.\n", search, middle+1);
         break;
      }
      else
         last = middle - 1;
 
      middle = (first + last)/2;
   }
   if (first > last)
      printf("Not found! %d isn't present in the list.\n", search);
 
   return 0;   
}
 


 Enter number of elements
10
Enter 10 integers
1
2
3
4
5
6
7
8
9
10
Enter value to find
6
6 found at location 6.
Press any key to continue . . .
 

Binary search is faster than linear search, but the list should be sorted, hashing is more rapid than binary search and perform searches in constant time.

Let's look at the differences in a tabular form.

Basis of comparison Linear search Binary search
Definition The linear search starts searching from the first element and compares each element with a searched element till the element is not found. It finds the position of the searched element by finding the middle element of the array.
Sorted data In a linear search, the elements don't need to be arranged in sorted order. The pre-condition for the binary search is that the elements must be arranged in a sorted order.
Implementation The linear search can be implemented on any linear data structure such as an array, linked list, etc. The implementation of binary search is limited as it can be implemented only on those data structures that have two-way traversal.
Approach It is based on the sequential approach. It is based on the divide and conquer approach.
Size It is preferrable for the small-sized data sets. It is preferrable for the large-size data sets.
Efficiency It is less efficient in the case of large-size data sets. It is more efficient in the case of large-size data sets.
Worst-case scenario In a linear search, the worst- case scenario for finding the element is O(n). In a binary search, the worst-case scenario for finding the element is O(log2n).
Best-case scenario In a linear search, the best-case scenario for finding the first element in the list is O(1). In a binary search, the best-case scenario for finding the first element in the list is O(1).
Dimensional array It can be implemented on both a single and multidimensional array. It can be implemented only on a multidimensional array.