| | | | | |

Binary Search Algorithm

  • Three versions:
    • binary_search
    • lower_bound
    • upper_bound
  • Assumptions:
    • collection v of data of type T and size sz
    • v is sorted (increasing order)
    • bracket operator provides random access to elements of v
    • element t of type T
  • Outcomes:
    • binary_search: return true if t is in v, false if not
    • lower_bound: return smallest index i such that t <= v[i]
    • upper_bound: return smallest index i such that t < v[i]

| | Top of Page | 3. Introduction to Algorithm Analysis - 7 of 23