Binary search

Computer Science · Algorithms

Question 1 of 10

0

    A sorted array has 28 items at indices 0 to 27. Use mid = (low + high) // 2 (integer division, rounded down). Indices start at 0. Binary search checks the middle item first. Which index does it check first?

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Binary search explained

    Binary search finds a target value in a sorted list by repeatedly halving the part of the list that could still contain it. It is much faster than checking every element in turn.

    The algorithm:

    1. Set low to the first index and high to the last index.
    2. While low is not greater than high: compute mid = (low + high) // 2 and compare the element at mid with the target.
    3. If they are equal, the target is found at mid.
    4. If the target is larger, it can only be to the right: set low = mid + 1.
    5. If the target is smaller, set high = mid - 1.
    6. If low passes high, the target is not in the list.

    Each comparison halves the remaining range, so binary search takes at most about log⁡2n\log_2 n comparisons: O(log⁡n)O(\log n). For a million items that is about 20 comparisons, against up to a million for a linear search.

    The list must be sorted. If it is not, binary search can miss items that are present; sorting first costs O(nlog⁡n)O(n \log n), which only pays off if you search many times.

    Worked example

    Search for 23 in [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] (indices 0 to 9).

    1. low = 0, high = 9, mid = 4: the element is 16. Since 23 > 16, set low = 5.
    2. low = 5, high = 9, mid = 7: the element is 56. Since 23 < 56, set high = 6.
    3. low = 5, high = 6, mid = 5: the element is 23. Found at index 5.

    Three comparisons instead of six for a linear scan.

    Key facts

    The practice questions use these rules:

    • Indices start at 0.
    • mid=⌊(low+high)÷2⌋\text{mid} = \lfloor (\text{low} + \text{high}) \div 2 \rfloor, integer division rounded down. For indices 0 to 7 the first probe is (0+7)//2=3(0 + 7) // 2 = 3.
    • If the middle item is too small, set low=mid+1\text{low} = \text{mid} + 1. If it is too big, set high=mid−1\text{high} = \text{mid} - 1.
    • Every look at a middle item counts as one comparison, including the one that finds the target.
    • The search stops when the item is found, or when low>high\text{low} > \text{high} (the range is empty).
    • For nn items, at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 comparisons are needed. For 1000 items that is 10. For a million items it is 20.

    Worked example 2

    Search for 35 in this sorted list (indices 0 to 10):

    3, 8, 12, 17, 21, 26, 30, 35, 41, 48, 55

    1. low = 0, high = 10. mid = 5, the value is 26. 26 < 35, so low = 6. (Comparison 1)
    2. low = 6, high = 10. mid = 8, the value is 41. 41 > 35, so high = 7. (Comparison 2)
    3. low = 6, high = 7. mid = 6, the value is 30. 30 < 35, so low = 7. (Comparison 3)
    4. low = 7, high = 7. mid = 7, the value is 35. Found at index 7. (Comparison 4)

    Four comparisons. A linear search would have taken 8.

    Worked example 3

    Search for 28 in the same list. It is not there.

    1. low = 0, high = 10. mid = 5, the value is 26. 26 < 28, so low = 6. (Comparison 1)
    2. low = 6, high = 10. mid = 8, the value is 41. 41 > 28, so high = 7. (Comparison 2)
    3. low = 6, high = 7. mid = 6, the value is 30. 30 > 28, so high = 5. (Comparison 3)
    4. Now low = 6 and high = 5, so low > high. The range is empty, so 28 is not in the list.

    An absent value is only known to be missing once the range is empty. So the answer is 3 comparisons, not the 4 you might expect from the longest case.

    Common mistakes

    • Using high = mid or low = mid. You must skip mid, because it has already been checked. Otherwise the loop can run forever.
    • Rounding mid up instead of down, which gives a different trace from the one expected.
    • Starting indices at 1 when the question says 0.
    • Forgetting that a search on an unsorted list may fail.
    • Stopping at low = high without checking that item.
    • Not counting the comparison that finds the target.

    Exam tips

    • Write a small trace table in your working: low, high, mid, value at mid, decision. Do this for every step.
    • Calculate mid on its own line each time. Do not do it in your head.
    • To check the number of comparisons, compare it with the worst case for the list size. For 11 items the most is 4.
    • Mention that the list must be sorted if the question asks why binary search can be used.
    • Say "halves the range each time" when explaining why it is faster than linear search.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      A sorted array has 42 items at indices 0 to 41. Use mid = (low + high) // 2 (integer division, rounded down). Indices start at 0. Binary search checks the middle item first. Which index does it check first?

    • Medium

      Binary search looks for 50 in this sorted list (indices 0 to 12): 1, 5, 12, 19, 23, 27, 30, 33, 39, 46, 50, 54, 56 Use mid = (low + high) // 2 (integer division, rounded down). Indices start at 0. How many comparisons does the search make before it finds 50? Count the comparison that finds it.

    • Hard

      Binary search looks for 41 in this sorted list (indices 0 to 13): 2, 5, 7, 13, 16, 21, 27, 33, 40, 42, 46, 52, 55, 57 Use mid = (low + high) // 2 (integer division, rounded down). Indices start at 0. How many comparisons does the search make before it can report that 41 is not in the list?