Understanding binary search

    In this tutorial, you will learn how binary search sorting works. In addition, you will find examples in C language.
    Binary search is a search algorithm used to find the position of an element in a sorted array.
    In this method, always search for an element in the middle of the partial array.
    Binary search can only be implemented on a sorted list. If the elements are not sorted yet, we need to sort them first.

How does binary search work?

    The binary search algorithm can be implemented in the following two ways.

  1. Iterative method
  2. Recursion

    The recursive method follows the divide and conquer method.
    The steps of these two methods are discussed below.

  1. The array to be searched is:
    Insert picture description here
    suppose x = 4 is the element to be searched.
  2. Set two pointers low and high at the lowest and highest positions respectively.
    Insert picture description here
  3. Find the middle element mid of the array, for example arr[(low + high)/2] = 6.
    Insert picture description here
  4. If x == mid, return mid, otherwise compare the searched element with mid.
  5. If x>mid, compare x with the middle element of the element to the right of mid. This can be done by setting low to low=mid+1.
  6. Otherwise, compare x with the middle element to the left of mid. This can be done by setting high to high=mid-1.
    Insert picture description here
  7. Repeat steps 3 to 6 until low meets high.
    Insert picture description here
  8. Find x = 4.
Binary search pseudo code

    Iterative method

do until the pointers low and high meet each other.
    mid = (low + high)/2
    if (x == arr[mid])
        return mid
    else if (x > arr[mid]) // x is on the right side
        low = mid + 1
    else                       // x is on the left side
        high = mid - 1

    Recursion

binarySearch(arr, x, low, high)
    if low > high
        return False 
    else
        mid = (low + high) / 2 
        if x == arr[mid]
            return mid
        else if x < arr[mid]        // x is on the right side
            return binarySearch(arr, x, mid + 1, high)
        else                               // x is on the right side
            return binarySearch(arr, x, low, mid - 1)
C example

    Iterative method

// Binary Search in C

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {
    
    
  // Repeat until the pointers low and high meet each other
  while (low <= high) {
    
    
    int mid = low + (high - low) / 2;

    if (array[mid] == x)
      return mid;

    if (array[mid] < x)
      low = mid + 1;

    else
      high = mid - 1;
  }

  return -1;
}

int main(void) {
    
    
  int array[] = {
    
    3, 4, 5, 6, 7, 8, 9};
  int n = sizeof(array) / sizeof(array[0]);
  int x = 4;
  int result = binarySearch(array, x, 0, n - 1);
  if (result == -1)
    printf("Not found");
  else
    printf("Element is found at index %d", result);
  return 0;
}

    Recursion

// Binary Search in C

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {
    
    
  if (high >= low) {
    
    
    int mid = low + (high - low) / 2;

    // If found at mid, then return it
    if (array[mid] == x)
      return mid;

    // Search the left half
    if (array[mid] > x)
      return binarySearch(array, x, low, mid - 1);

    // Search the right half
    return binarySearch(array, x, mid + 1, high);
  }

  return -1;
}

int main(void) {
    
    
  int array[] = {
    
    3, 4, 5, 6, 7, 8, 9};
  int n = sizeof(array) / sizeof(array[0]);
  int x = 4;
  int result = binarySearch(array, x, 0, n - 1);
  if (result == -1)
    printf("Not found");
  else
    printf("Element is found at index %d", result);
}
Binary search complexity

    time complexity

  • Best case complexity: O(1)
  • Average case complexity: O(log n)
  • Worst case complexity: O(log n)

    Space complexity The space complexity of
    binary search is O(n).

Application of binary search
  • Used in Java, .Net, C++ STL library
  • During debugging, binary search is used to determine where the error occurred
Reference documents

[1]Parewa Labs Pvt. Ltd.Binary Search[EB/OL].https://www.programiz.com/dsa/binary-search,2020-01-01.

Guess you like

Origin blog.csdn.net/zsx0728/article/details/115047976