liveBook · Manning

Binary search

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A binary search locates a value in an ordered, index-accessible collection by repeatedly comparing with the middle element and halving the candidate range.

Binary search locates a value in a sorted, index-accessible collection by checking the middle element and discarding the half that cannot contain the target. section 2.1

Apply the technique by maintaining a candidate range:

  • Set low to the first index and high to the last index. section 2.1
  • While low is no greater than high, calculate the middle index. section 2.1
  • If the middle value is less than the target, move low above it; if greater, move high below it. section 2.1
  • Return true on equality and false when the range becomes empty. section 2.1

Searching sorted words

In ["cat", "dog", "kangaroo", "llama", "rabbit", "rat", "zebra"], checking "llama" shows that "rat" must lie in the later half. Repeating the comparison narrows the range until "rat" is found. A Swift implementation generalizes this idea with binaryContains<T: Comparable>. section 2.1

func binaryContains(_ array: Gene, item: Codon) -> Bool {
    var low = 0
    var high = array.count - 1
    while low <= high {
        let mid = (low + high) / 2
        if array[mid] < item {
            low = mid + 1
        } else if array[mid] > item {
            high = mid - 1
        } else {
            return true
        }
    }
    return false
}

The complete Swift implementation makes the shrinking bounds, midpoint calculation, comparison branches, and termination condition directly inspectable. section 2.1

The method requires ordering and constant-time index access, making a sorted Swift Array suitable. It can search sorted Gene data after sorting it, while an unsorted collection does not meet the stated conditions. section 2.1

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Explains the halving process, range updates, termination, generic Swift implementation, and a performance exercise without formal proof.
Examples
Demonstrates sorted word lookup, Gene and Codon lookup, generic arrays, and timing searches across one million numbers.
What it assumes
Assumes ordered data and index access, while demonstrating the required comparisons and range updates explicitly.
What you can do after
Apply it via Swift by implementing and timing generic binaryContains functions on sorted arrays.
How it connects
Connects binary search with linear search, sorted arrays, Comparable values, and search algorithm selection.

Also explained in

Compare how 7 books explain binary search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.