In computer science, 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. Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. If the search ends with the remaining half being empty, the target is not in the array.

Binary Search

Complexity

Time Complexity: O(log(n)) - since we split search area by two for every next iteration.

References

Implementations

  • 1
    
    # TODO: Implement in Python
    
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    
    function BinarySearch(array, element) { // array = sorted array, element = element to be found
      let minIndex = 0;
      let maxIndex = array.length - 1;
      let testElement;
    
      while (minIndex <= maxIndex) {
        const middleIndex = Math.floor((minIndex + maxIndex) / 2);
        testElement = array[middleIndex];
    
        if (testElement < element) {
          
          console.log('Going right.');
          
          minIndex = middleIndex + 1;
        } else if (testElement > element) {
          
          console.log('Going left.');
          
          maxIndex = middleIndex - 1;
        } else {
    
          return middleIndex;
        }
      }
    
      console.log(`${element} is not found!`);
      
      return -1;
    }
    
    const element = D[Randomize.Integer({ min: 0, max: D.length - 1 })];
    
    console.log(`Using iterative binary search to find ${element}`);
    
    BinarySearch(D, element);
    
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    
    function BinarySearch(array, element, minIndex, maxIndex) { // array = sorted array, element = element to be found, minIndex = low index, maxIndex = high index
      if (minIndex > maxIndex) {
        
        console.log(`${element} is not found!`);
        
        return -1;
      }
    
      const middleIndex = Math.floor((minIndex + maxIndex) / 2);
      const testElement = array[middleIndex];
    
      if (testElement < element) {
        
        console.log('Going right.');
        
        return BinarySearch(array, element, middleIndex + 1, maxIndex);
      }
    
      if (testElement > element) {
        
        console.log('Going left.');
        
        return BinarySearch(array, element, minIndex, middleIndex - 1);
      }
    
      if (testElement === element) {
        
        return middleIndex;
      }
    
      console.log(`${element} is not found!`);
      
      return -1;
    }
    
    const element = D[Randomize.Integer({ min: 0, max: D.length - 1 })];
    
    console.log(`Using binary search to find ${element}`);
    
    BinarySearch(D, element, 0, D.length - 1);