Searching algorithms: linear and binary search

10 questions41 marksabout 50 min

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

1Grade 1–33 marks

(a) State one requirement that the data must meet before a binary search can be used. [1]

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

(c) A linear search is carried out on an unsorted list of 1000 items. State the maximum number of items that may need to be checked. [1]

2Grade 4–53 marks

A list of numbers is shown.

[3, 8, 12, 19, 25, 34, 41, 47, 50]

Show the stages of a binary search to find the number 34. [3]

3Grade 6–75 marks

Write a function called linearSearch that:

• takes an array items and a value target as parameters

• returns the index of the first position where target is found

• returns -1 if target is not in the array.

Do not use a built-in search function.

Use OCR Exam Reference Language or a high-level programming language. [5]

Stuck on Searching algorithms: linear and 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 1–34 marks

A list of names is stored in an array.

names = ["Zara", "Ben", "Omar", "Lia", "Tom", "Ria"]

(a) A linear search is used to find "Omar". Write down the names that are compared, in the order they are compared, and state how many comparisons are made. [2]

(b) A linear search is then used to find "Kai". State how many comparisons are made before the search stops. [1]

(c) Explain why a binary search could not be used on this array as it is stored. [1]

5Grade 1–34 marks

Two systems each need to search for data.

System 1: A library catalogue holds 50 000 book records stored in order of ISBN. A librarian types in an ISBN to find the record.

System 2: A door scanner holds a list of 20 visitor names in the order the visitors arrived. The scanner checks whether a name is on the list.

(a) State which searching algorithm is most suitable for System 1 and justify your choice. [2]

(b) State which searching algorithm is most suitable for System 2 and justify your choice. [2]

6Grade 4–53 marks

A function is written in OCR Exam Reference Language.

function find(items, target)
    low = 0
    high = items.length - 1
    while low <= high
        mid = (low + high) DIV 2
        if items[mid] == target then
            return mid
        elseif items[mid] < target then
            low = mid + 1
        else
            high = mid - 1
        endif
    endwhile
    return -1
endfunction

(a) State the name of the searching algorithm that this function performs. [1]

(b) Identify two features of the code that show this is the algorithm you named in (a). [2]

7Grade 6–75 marks

A sorted array data has 12 items.

index01234567891011
value259142027333845516072

A binary search is carried out for the value 40. The algorithm uses low and high as the indexes of the ends of the part of the array still being searched, and calculates mid = (low + high) DIV 2.

(a) Complete the trace table. The first row has been done for you. Add as many rows as are needed. [4]

lowhighmiddata[mid]
011527
 
 
 
 

(b) State how the algorithm knows that 40 is not in the array. [1]

8Grade 4–54 marks

The flowchart shows a binary search of a sorted array items for a value target. Two parts of the flowchart, labelled A and B, are blank.

(a) State what should be written in box A. [1]

(b) State what should be written in box B. [1]

(c) A sorted array has 4096 items. State the maximum number of times the remaining part of the array can be halved before only one item is left. [1]

(d) Explain what could go wrong if this flowchart is used on an array that is not sorted. [1]

Startlow = 0, high = length − 1low ≤ high?NoYesOutput "Not found"mid = (low + high) DIV 2AYesNoOutput "Found"items[mid] < target?YesNoBhigh = mid − 1Stop
9Grade 4–54 marks

A student writes a linear search in OCR Exam Reference Language. It should print the index of target once if it is in the array, or print "Not found" once if it is not.

scores = [14, 7, 22, 9, 31]
target = 9
for i = 0 to scores.length - 1
    if scores[i] == target then
        print("Found at " + str(i))
    else
        print("Not found")
    endif
next i

(a) Write down the exact output produced when this code is run. [2]

(b) Describe how the code should be changed so that it works correctly. [2]

10Grade 8–96 marks

Write a function called contains that:

• takes a sorted array of integers items and an integer target as parameters

• uses a binary search to decide whether target is in the array

• returns true if target is found and false otherwise.

Reminder: a binary search repeatedly looks at the middle item of the part of the array still being searched and, if it is not the target, discards the half that cannot contain the target.

You must not use a built-in search function, and a linear search will not gain full marks.

Use OCR Exam Reference Language or a high-level programming language. [6]

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