Daniel Lyons' Notes

Binary Search

Algorithm

  • Given:

      1. array of values
      1. a target value
  • Find: the index for the target value

  • Precondition: The values must be presorted

Pseudo code

  1. Find the midpoint value
  2. left = start index
  3. right = end index
  4. Loop:
    1. middleIndex = midpoint between left and right
    2. lookup middleValue at middleIndex
    3. If: middleValue value > target:
      1. then: decrement right
    4. else if: middleValue < target
      1. then: increment left
    5. else if middleValue == target
      1. then: return the index
Binary Search
Interactive graph
On this page
Algorithm
Pseudo code