Binary Search :

Binary search,also known as half-interval search, logarithmic search, or binary chop,is a search algorithm that finds the position of a target value within a sorted array.[4][5] Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. Even though the idea is simple, implementing binary search correctly requires attention to some subtleties about its exit conditions and midpoint calculation. Binary search runs in logarithmic time in the worst case, making O(log n) comparisons, where n is the number of elements in the array, the O is Big O notation, and log is the logarithm. Binary search takes constant (O(1)) space, meaning that the space taken by the algorithm is the same for any number of elements in the array.Binary search is faster than linear search except for small arrays, but the array must be sorted first. Although specialized data structures designed for fast searching, such as hash tables, can be searched more efficiently, binary search applies to a wider range of problems.


Binary Search Visualization

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10]
0 1 2 3 4 5 6 7 8 9 10


 

      



Trace
  • procedure binary_search (list, value)
  •  while low <=high
  •    mid = (low + high) / 2
  •        if (A[mid] < value)
  •            low = mid + 1
  •        else if (A[mid] > value)
  •            high = mid - 1
  •        else
  •             retrun mid
  •  end while
  • end porcedure




Binary Search Code in C:

                      
                          #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;  
                          }