Linear Search and Binary Search with Tracing (ICSE Class 10 Java)
Updated 17 October 2026 · Fact-checked
Linear search compares the key with each element from the start until it is found or the array ends; it works on any array. Binary search works only on a sorted array: it compares the key with the middle element and discards the half that cannot contain it, repeating until the key is found or low becomes greater than high. The middle index is mid = (low + high) / 2.
Each step checks the middle (orange) and discards half the array (grey), so key 25 is found in just 3 steps.
Illustration drawn for QuestionBuddy.
Understand Linear Search and Binary Search Tracing
Linear search. Check the elements one by one:
int pos = -1;
for (int i = 0; i < a.length; i++)
if (a[i] == key) { pos = i; break; }If the key is at index k it takes k + 1 comparisons; if it is absent it takes n. It needs no sorting, so it is slow for big arrays but always safe.
Binary search (array must be sorted).
int low = 0, high = a.length - 1, pos = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid] == key) { pos = mid; break; }
else if (a[mid] < key) low = mid + 1;
else high = mid - 1;
}Each step halves the search range. Integer division in (low + high) / 2 drops the fraction. The loop ends when the key is found (pos is set) or when low > high (not found, pos stays -1).
Trace on {3, 8, 12, 19, 25, 31, 40} (indices 0 to 6), key 25:
- low 0, high 6, mid 3 → 19 < 25, so low = 4
- low 4, high 6, mid 5 → 31 > 25, so high = 4
- low 4, high 4, mid 4 → 25 found at index 4 (3 steps)
Key 10 (absent): mid 3 (19 > 10) → high 2; mid 1 (8 < 10) → low 2; mid 2 (12 > 10) → high 1; now low 2 > high 1, so not found.
Number of steps. For an array of n elements binary search needs at most about log base 2 of n steps (for 7 elements at most 3; for 1000 elements about 10), while linear search may need n.
Choosing. Unsorted data: linear search (or sort first). Sorted data: binary search. Binary search on an unsorted array can miss a key that is present.
Linear Search and Binary Search Tracing: key facts
- Linear search
- Check from index 0
- Stop when a[i] equals key
- Found at index k: k + 1 comparisons
- Absent: n comparisons
- Works on any array.
- Binary search
- Array must be sorted
- mid = (low + high) / 2
- Key smaller: high = mid - 1
- Key larger: low = mid + 1
- Stop when low is greater than high
- Integer division for mid.
- Maximum steps of binary search
n = 7 3 n = 15 4 n = 1000 about 10 - Maximum steps = floor(log2 n) + 1. For example, n = 8 needs at most 4 steps, since log2 8 = 3.
- Not found marker
- pos = -1 at the start
- Set pos = index when found
- pos still -1 means not found
- A common convention in programs.
How to answer Linear Search and Binary Search Tracing questions
Use this routine for tracing a search.
- 1Write the array with its indices; for binary search check that it is sorted.
- 2Linear: move i from 0, compare a[i] with the key at each step, stop at the first match or at the end.
- 3Binary: set low = 0, high = n - 1, then compute mid = (low + high) / 2 using integer division.
- 4Compare a[mid] with the key; move low to mid + 1 or high to mid - 1; recompute mid.
- 5Stop when found or when low is greater than high; count the steps.
Linear Search and Binary Search Tracing in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Binary search as a dictionary look-up
Suppose a student looks up a word in a dictionary by opening near the middle, deciding whether the word comes before or after, and repeating on that half.
Which search is this, and why does it need the pages to be in order?
Try it first, then show the solutionSolution
- Halving the range each time is binary search.
- It relies on the order: knowing whether the word is before or after tells which half to discard.
Answer: Binary search; it needs a sorted array.
Memory hook: Open in the middle, discard half.
Tricks to remember Linear Search and Binary Search Tracing
Short memory aids. Say each one aloud once, then use it on a question the same day.
Low-High-Mid
Write low, high and mid in three columns and fill a row per step.
Use it when: Binary search traces.
Smaller means go left
If the key is smaller than a[mid], high = mid - 1; if larger, low = mid + 1.
Use it when: Direction of the search.
Sorted first
No sorting, no binary search.
Use it when: Choosing a method.
Linear Search and Binary Search Tracing: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: When to use which search · 3 marks · Short answerBoard level
A student says "binary search is always better than linear search". Give two situations in which linear search must or may be preferred, and one in which binary search is better.
Try it first, then show the solutionSolution
- Linear search must be used when the array is not sorted, because binary search needs sorted data. (1 mark)
- Linear search may be preferred for a very small array, or when sorting just for one search would cost more than searching. (1 mark)
- Binary search is better for a large sorted array searched many times, since each step halves the range. (1 mark)
Answer: Linear: unsorted or very small arrays. Binary: large sorted arrays.
Example 2: Linear search with a count · 2 marks · Short answerHarder board level
Write the output:
int[] a = {7, 3, 9, 3, 5};
int key = 3, first = -1, count = 0;
for (int i = 0; i < a.length; i++)
if (a[i] == key) {
count++;
if (first == -1) first = i;
}
System.out.println(first + " " + count);Try it first, then show the solutionSolution
- The key 3 occurs at indices 1 and 3, so count = 2. (1 mark)
- first is set only the first time: 1. Output 1 2. (1 mark)
Answer: 1 2
Example 3: Binary search that finds the key · 3 marks · Short answerHarder board level
Write the output:
int[] a = {3, 8, 12, 19, 25, 31, 40};
int key = 25, low = 0, high = a.length - 1, pos = -1;
while (low <= high) {
int mid = (low + high) / 2;
System.out.print(mid + " ");
if (a[mid] == key) { pos = mid; break; }
else if (a[mid] < key) low = mid + 1;
else high = mid - 1;
}
System.out.println(pos);Try it first, then show the solutionSolution
- Step 1: low 0, high 6, mid 3; a[3] = 19 < 25, so low = 4. Step 2: mid = (4 + 6) / 2 = 5; a[5] = 31 > 25, so high = 4. (1 mark)
- Step 3: mid = (4 + 4) / 2 = 4; a[4] = 25 equals the key, so pos = 4 and the loop breaks. (1 mark)
- The mids 3 5 4 are printed with spaces, then pos: 3 5 4 4. (1 mark)
Answer: 3 5 4 4
Example 4: Case study: a key that is not present · 4 marks · Case-basedHOTS / competency
The same program is run on the same array with key = 10.
(i) State the output. (2)
(ii) Why does the loop end? (1)
(iii) What is the greatest number of mid values that can be examined for an array of 7 elements? (1)
Try it first, then show the solutionSolution
- (i) mid = 3 (19 > 10) so high = 2; mid = 1 (8 < 10) so low = 2; mid = 2 (12 > 10) so high = 1. Printed mids: 3 1 2, then pos = -1: output 3 1 2 -1.
- (ii) Now low = 2 is greater than high = 1, so the condition low <= high is false and the key is declared absent.
- (iii) Each step halves the range: 7, 3, 1 elements, so at most 3 mid values.
Answer: (i) 3 1 2 -1 (ii) low became greater than high (iii) 3
Example 5: Comparing the work · 3 marks · Short answerHarder board level
For the key 40 in {3, 8, 12, 19, 25, 31, 40}, how many comparisons does linear search need and how many steps does binary search need? Show the mid values.
Try it first, then show the solutionSolution
- Linear search finds 40 at index 6, after 7 comparisons. (1 mark)
- Binary search: low 0, high 6, mid 3 (19 < 40) so low = 4; mid 5 (31 < 40) so low = 6; mid 6 (40 found). (1 mark)
- Binary search needs 3 steps, with mid values 3, 5 and 6. (1 mark)
Answer: Linear: 7 comparisons; binary: 3 steps (mid 3, 5, 6)
Where students lose marks in Linear Search and Binary Search Tracing
The slips examiners see most often, and what to write instead.
Mistake: Applying binary search to an unsorted array
The decision to discard half the array is only valid when the data is in order.
Wrong working
Binary search on {9, 2, 7, 4}
Right working
Sort first, then search; or use linear search
Do this: Check the order first.
Usually costs: 1 to 2 marks
Mistake: Using mid instead of mid + 1 or mid - 1
The element at mid has already been checked; low = mid can loop for ever.
Wrong working
low = mid;
Right working
low = mid + 1;
Do this: Always move past mid.
Usually costs: 1 mark
Mistake: Writing the loop condition as low < high
When one element is left, low equals high and it must still be checked.
Wrong working
while (low < high)
Right working
while (low <= high)
Do this: Use <=.
Usually costs: 1 mark
Exam tips
- Draw a low, high, mid table for trace questions.
- State that binary search needs a sorted array, in every comparison answer.
- Use pos = -1 as a not-found marker and print a message when it remains -1.
- Mention the number of steps to show you counted them.
Practice questions on Linear Search and Binary Search Tracing
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
Binary search can be applied to an array only if the array is:
(a) of integers only (b) sorted (c) of size less than 10 (d) two-dimensionalHint
Why can half of the array be discarded?
Answer
(b) sorted
Q2 · 1 mark · Very short answerBoard level
Write the formula used to find the middle index in binary search.
Hint
Uses low and high.
Answer
mid = (low + high) / 2
Q3 · 2 marks · Short answerHarder board level
Write the output of the binary search program of the worked example with key 8 on {3, 8, 12, 19, 25, 31, 40}.
Hint
First mid is 3.
Answer
3 1 1 (mid 3: 19 > 8 so high = 2; mid 1: 8 found, pos = 1)
Q4 · 3 marks · Short answerHarder board level
Write a static method int find(int[] a, int k) that returns the index of the first occurrence of k using linear search, or -1 if absent. What are find(new int[]{4, 9, 2}, 2) and find(new int[]{4, 9, 2}, 7)?
Hint
Return inside the loop.
Answer
static int find(int[] a, int k) {
for (int i = 0; i < a.length; i++)
if (a[i] == k) return i;
return -1;
}
find for key 2 returns 2; for key 7 returns -1.Q5 · 1 mark · Assertion-reasonHOTS / competency
Assertion (A): Binary search is faster than linear search on a large sorted array.
Reason (R): Each step of binary search halves the portion of the array that remains to be searched.
(a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.Hint
Compare the number of steps.
Answer
(a) Both are true and R explains A.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.What does binary search require?Show answer
A sorted array.
2.What is mid for low = 0 and high = 6?Show answer
3
3.How many comparisons does linear search need if the key is at index 4?Show answer
5
4.When does binary search stop with not found?Show answer
When low becomes greater than high.
5.If key is smaller than a[mid], which pointer moves?Show answer
high = mid - 1.
6.Which search works on unsorted data?Show answer
Linear search.
Linear Search and Binary Search Tracing: frequently asked questions
Does binary search work for descending arrays?
Yes, if the comparisons are reversed. The ICSE programs use ascending order.
What is the value of mid when low + high is odd?
Integer division drops the fraction, e.g. (0 + 5) / 2 = 2.
What if the key occurs more than once?
Linear search finds the first occurrence; binary search finds one of them.