SSVISUAL.COM

Binary Search

It's like finding a book in a library by repeatedly dividing the books in half, until you locate the one you want.

Pseudocode

function binary_search(list, target):
  left = 0
  right = length(list) - 1
  WHILE left <= right:
    mid = (left + right) // 2
    IF list[mid] == target:
      RETURN mid
    ELIF list[mid] < target:
      left = mid + 1
    ELSE:
      right = mid - 1
    END IF
  END WHILE
                

Terminal

Left Index:

Middle Index:

TARGET