← Back

Binary Search

Halve the search space each step to find a value in a sorted array in O(log n).

1 / 1
func binarySearch(nums []int, target int) int {

}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {

    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
        mid := lo + (hi-lo)/2
    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
        mid := lo + (hi-lo)/2

        // Three outcomes, decided by comparing nums[mid] to target.
    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
        mid := lo + (hi-lo)/2

        // Three outcomes, decided by comparing nums[mid] to target.
        switch {

        }
    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
        mid := lo + (hi-lo)/2

        // Three outcomes, decided by comparing nums[mid] to target.
        switch {
        // 1) Direct hit — mid is the index we're after.
        }
    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
        mid := lo + (hi-lo)/2

        // Three outcomes, decided by comparing nums[mid] to target.
        switch {
        // 1) Direct hit — mid is the index we're after.
        case nums[mid] == target:
            return mid
        }
    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
        mid := lo + (hi-lo)/2

        // Three outcomes, decided by comparing nums[mid] to target.
        switch {
        // 1) Direct hit — mid is the index we're after.
        case nums[mid] == target:
            return mid

        // 2) Middle too small — target must be to the RIGHT.
        //    mid is already ruled out, so start the new window at mid+1.
        //    (Excluding mid also guarantees the window shrinks — no infinite loop.)
        }
    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
        mid := lo + (hi-lo)/2

        // Three outcomes, decided by comparing nums[mid] to target.
        switch {
        // 1) Direct hit — mid is the index we're after.
        case nums[mid] == target:
            return mid

        // 2) Middle too small — target must be to the RIGHT.
        //    mid is already ruled out, so start the new window at mid+1.
        //    (Excluding mid also guarantees the window shrinks — no infinite loop.)
        case nums[mid] < target:
            lo = mid + 1
        }
    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
        mid := lo + (hi-lo)/2

        // Three outcomes, decided by comparing nums[mid] to target.
        switch {
        // 1) Direct hit — mid is the index we're after.
        case nums[mid] == target:
            return mid

        // 2) Middle too small — target must be to the RIGHT.
        //    mid is already ruled out, so start the new window at mid+1.
        //    (Excluding mid also guarantees the window shrinks — no infinite loop.)
        case nums[mid] < target:
            lo = mid + 1

        // 3) Otherwise middle is too big — target must be to the LEFT.
        //    Symmetrically, drop mid by moving hi to mid-1.
        default:
            hi = mid - 1
        }
    }
}
func binarySearch(nums []int, target int) int {
    // Micropattern: track the still-possible window [lo, hi], inclusive on both ends.
    lo, hi := 0, len(nums)-1

    // Keep going while the window is non-empty. Use <= because hi is inclusive:
    // when lo == hi there's still one element left to check.
    for lo <= hi {
        // Inspect the middle. Write lo+(hi-lo)/2, not (lo+hi)/2, to avoid overflow.
        mid := lo + (hi-lo)/2

        // Three outcomes, decided by comparing nums[mid] to target.
        switch {
        // 1) Direct hit — mid is the index we're after.
        case nums[mid] == target:
            return mid

        // 2) Middle too small — target must be to the RIGHT.
        //    mid is already ruled out, so start the new window at mid+1.
        //    (Excluding mid also guarantees the window shrinks — no infinite loop.)
        case nums[mid] < target:
            lo = mid + 1

        // 3) Otherwise middle is too big — target must be to the LEFT.
        //    Symmetrically, drop mid by moving hi to mid-1.
        default:
            hi = mid - 1
        }
    }

    // lo passed hi: the window is empty, so target isn't here. O(log n) overall.
    return -1
}

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.