Question bank › GCSE › GCSE Computer Science › Fundamentals of algorithms

Searching algorithms: linear & binary search

10 questions49 marksabout 60 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Grade 4–54 marks

A binary search is used to find the value 63 in this sorted list:

[4, 9, 15, 22, 28, 33, 41, 50, 56, 63, 70]

(a) List, in order, each value that is compared with 63 during the search. [3]

(b) Explain why a binary search could not be used on the list [22, 4, 70, 9, 41]. [1]

2Grade 8–96 marks

An array names holds a list of student names. Write an algorithm, using either pseudo-code or program code in the language you are studying (C#, Python or VB.NET), that:

  • asks the user to enter a name to search for
  • uses a linear search to find the first position of that name in names
  • outputs the index of the name if it is found, or outputs −1 if it is not in the array
  • stops searching as soon as the name is found.

[6]

3Grade 1–34 marks

A linear search is used on this unsorted list to find the value 61:

[17, 42, 8, 96, 23, 61, 35, 50]

(a) State how many values are compared with 61 before the search stops. [1]

(b) The search is repeated for the value 99, which is not in the list. State how many comparisons are made. [1]

(c) Describe how a linear search works. [2]

Stuck on Searching algorithms: linear & binary search?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Grade 4–55 marks

This list of names is stored in alphabetical order:

["Amir", "Bella", "Chen", "Dev", "Ella", "Finn", "Grace", "Hugo", "Isla", "Jack"]

A binary search is used to look for "Chen". When there are two middle items the search uses the left (lower) one.

(a) List, in order, the names that are compared with "Chen". [2]

(b) The search is repeated for "Dan", which is not in the list. State how many names are compared with "Dan" before the search reports that it is not found. [1]

(c) Explain how the algorithm knows that "Dan" is not in the list. [2]

5Grade 1–34 marks

A shop's till holds a list of 40 product codes that is not in any order. The list is searched once a day.

(a) State which searching algorithm should be used, and give a reason for your choice. [2]

(b) State one advantage of a binary search over a linear search. [1]

(c) State one disadvantage of a binary search compared with a linear search. [1]

6Grade 6–75 marks

A sorted array holds 1000 customer IDs.

(a) State the maximum number of comparisons a linear search would need to find an ID that is in the array. [1]

(b) Show that a binary search needs at most 10 comparisons to find any ID in this array. [2]

(c) The array grows to 1 000 000 IDs. State the maximum number of comparisons a binary search would now need, and explain why the increase is much smaller than for a linear search. [2]

7Grade 6–76 marks

The pseudo-code below performs a binary search on the array nums.

nums ← [3, 8, 12, 20, 27, 31, 45]
target ← 12
low ← 0
high ← LEN(nums) - 1
found ← False
WHILE low ≤ high AND found = False
   mid ← (low + high) DIV 2
   IF nums[mid] = target THEN
      found ← True
   ELSE IF nums[mid] < target THEN
      low ← mid + 1
   ELSE
      high ← mid - 1
   ENDIF
ENDWHILE
OUTPUT mid

(a) Complete the trace table for this algorithm. Write one row for each time the WHILE loop body runs. [4]

lowhighmidnums[mid]found
06

(b) State the value that is output. [1]

(c) State what the program would do if target were 50, which is not in the array. [1]

8Grade 4–55 marks

A program is meant to use a linear search to find the position of a score in the array scores and output that position.

scores ← [55, 72, 48, 91, 66, 72]
target ← USERINPUT
i ← 0
found ← False
position ← -1
WHILE i < LEN(scores) AND found = False
   IF scores[i] = target THEN
      found ← True
      position ← i
   ENDIF
ENDWHILE
OUTPUT position

(a) The program has a logic error. Describe what happens when the user enters 72, and explain why. [2]

(b) Write the line of pseudo-code needed to correct the error and state exactly where it should go. [2]

(c) After the correction, state the value output when the user enters 72. [1]

9Grade 8–96 marks

A streaming service stores 50 million song titles. Users search for a title by typing its exact name, and the service receives thousands of searches every second.

Compare the use of a linear search and a binary search for this service, and recommend which the service should use. In your answer you should refer to how each algorithm works, the data it needs and the number of comparisons involved. [6]

10Grade 4–54 marks

The diagram shows a sorted array numbers of 15 items.

A binary search is used to find the value 47. The middle item is found using mid ← (low + high) DIV 2.

(a) State the index of the first item that is compared with 47. [1]

(b) List, in order, the indexes of all the items compared with 47 during the search. [2]

(c) State the maximum number of comparisons a binary search would need to find any value in this array. [1]

indexvalue02152931141451862172582993310381142124713511458sorted array numbers (15 items)

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session