Code Companion
Java

Programming technique · B2.4.2

Linear and binary search

Linear search checks candidates in sequence. Binary search repeatedly discards half of a sorted search interval. Both must be constructed and traced accurately.

IB DP CS standard B2.4.2: Construct and trace linear and binary search algorithms for data retrieval, compare their efficiency and choose a search technique based on efficiency requirements.

Linear search: inspect values in order

Linear search starts at the first position and checks each value until the target is found or no positions remain. It works on unsorted data.

Target 44 in [12, 25, 31, 44, 57]Index checkedValueResult
1012continue
2125continue
3231continue
4344return 3

Binary search: sorted data is a precondition

Do not skip this: the approved model assumes ascending sorted data. If the data is unsorted, comparing with the middle value does not tell you which half can be discarded.
Target 57 in [12, 18, 25, 31, 44, 57, 63, 79]lowhighmiddlemiddle valueAction
10733157 is larger → keep indexes 4–7
247557return 5

Understand the boundaries

low and high are inclusive

Start at index 0 and length - 1. The current interval includes both boundaries.

Discard the middle too

After a failed middle comparison, use middle + 1 or middle - 1; otherwise the same position may be checked forever.

Index 0 is a valid match

Use -1 for not found so a match at the first position cannot be confused with failure.

Duplicates are allowed

Linear search returns the first match it encounters from the left. This binary search returns a matching index but does not promise the first or last duplicate.

Check your understanding

Answer each question before opening the model answer.

  1. Can linear search correctly search an unsorted list?

    Reveal model answer

    Yes. It checks candidate values directly and does not depend on their order.

  2. Why can binary search discard half of the current interval?

    Reveal model answer

    Because the data is sorted. Comparing the target with the middle value proves that one whole half cannot contain the target.

  3. What is wrong with high = values.length for an inclusive high boundary?

    Reveal model answer

    The final valid index is values.length - 1. Using values.length creates an out-of-range boundary.

  4. Which has the slower worst-case growth on large inputs: O(n) linear search or O(log n) binary search?

    Reveal model answer

    O(n) grows faster. Binary search is generally more scalable for large sorted data, but sorting or maintaining order also has a cost.

Efficiency supports the choice

QuestionLinear searchBinary search
Sorted data required?NoYes
Worst-case timeO(n)O(log n)
Auxiliary space, iterative modelO(1)O(1)
Good fitSmall/unsorted/changing data or a one-off searchLarge sorted data or repeated searches where order is already maintained
Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Search Comparison

Challenge ID: PC-T14-C01 · Standards: B2.4.2

Construct linearSearch and iterative binarySearch methods that return the matching index or -1. Use the supplied sorted data, then add tests for the first value, a middle value, the last value, an absent value and an empty array. Trace at least one successful and one unsuccessful binary search on paper before running the code.

Scaffold available

Asset Tag Finder

Challenge ID: PC-T14-C02 · Standards: B2.4.2, B2.4.1

Store a set of integer equipment asset tags. Implement both search methods yourself rather than using a built-in search. Count the comparisons made for several targets and compare the worst-case growth of linear and binary search. Explain why binary search is only valid after the tags are kept in sorted order.