Community resourceWorksheet
J277 2.1 Algorithms consolidation
Part 4 of 4 · J277 2.1.3 · Searching and sorting
The Block B checkpoint, combining strings, arrays, subprograms, searching and sorting.
Students will:
- select a searching or sorting algorithm to suit the data
- combine strings, arrays and subprograms in one solution
- read unfamiliar code and predict its output
- debug a premature return
- justify an algorithm choice in exam terms
Inside: 5 explanation cells, 3 runnable Python tasks, 3 multiple-choice questions and 5 written answers. 28 marks, about 60 minutes.
Series: J277 2.1.3 · Searching and sorting, part 4 of 4.
Shared by Coding PathwayVerified teacher
- 16 cells
- About 60 minutes
- CC BY-SA 4.0
- Shared 17 Aug 2026
- Updated 9 Sept 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Algorithms consolidation
This worksheet brings together strings, arrays, subprograms, searching and sorting. The questions move from recognition to program completion and applied exam-style reasoning. Read each scenario carefully and choose methods because they fit the data and requirement.
Key ideas to recall
- A linear search checks items in order and can be used on unsorted data.
- A binary search repeatedly checks the middle item and discards half of the remaining search area. Its data must already be sorted.
- A bubble sort compares adjacent values and swaps those in the wrong order.
- An insertion sort takes the next unsorted value and inserts it into the correct place in a growing sorted section.
- A merge sort splits data into smaller sublists, then merges sorted sublists back together.
- String characters and array-style list elements can be selected using an index; Python starts indexing at
0. - A function uses
returnwhen its result needs to be stored or used elsewhere in a program.
A list of unsorted emergency call identifiers must be checked once for a target. Which search is immediately applicable?
- ABinary search
- BMerge sort
- CInsertion sort
- DLinear search
Which algorithm repeatedly inserts the next unsorted value into a growing sorted section?
- AInsertion sort
- BBubble sort
- CLinear search
- DBinary search
Apply one complete ascending bubble-sort pass to [5, 1, 4, 2]. Show the list after each swap.
Compare adjacent values from left to right. Swap them only when the left value is larger.
Students type their answer here.
Integrated practice: strings, arrays and functions
Complete count_initial(names, target_initial) so it returns how many names begin with the required character. Loop through every name, use index 0 to select its first character and increase count when it matches the parameter.
Replace pass with the required decision. The function must work for different lists and initials, so do not hard-code "A" or the answer 2.
# Complete the function to return the number of names beginning with target_initial.
def count_initial(names, target_initial):
count = 0
for name in names:
pass
return count
answer = count_initial(["Aisha", "Ben", "Amir", "Cara"], "A")
A school keeps student surnames in alphabetical order and frequently searches for a surname. Recommend a search algorithm and explain its steps for this scenario.
Name the prerequisite already satisfied, then explain how the search area changes.
Students type their answer here.
Read and run: finding the longest string
This function combines an array parameter, initialisation from the first element, iteration, selection and a returned result. Predict the word returned before running it.
Notice that longest stores an actual string rather than merely its length. The comparison uses lengths, but the function returns the selected element from the array.
def find_longest(words):
longest = words[0]
for word in words:
if len(word) > len(longest):
longest = word
return longest
result = find_longest(["oak", "willow", "ash", "beech"])
print(result)
Explain how the find_longest function works. Refer to its parameter, initial value, loop, selection and returned value.
Follow the data through the function rather than translating each line separately.
Students type their answer here.
Why is longest = words[0] safer than longest = "" when the task is to return an element from a non-empty list?
- AIt automatically sorts the words
- BIt guarantees the starting candidate is an actual list element
- CIt makes every string the same length
- DIt prevents the loop from running
Debugging challenge: a premature return
The supplied contains function should return True when target appears anywhere in values, and False only after every element has been checked.
At present, the else returns False after the first non-match, ending the function too early. Move the unsuccessful return to the correct position while preserving the successful return inside the loop.
# Debug the function. It should return True when target is present and False otherwise.
def contains(values, target):
for value in values:
if value == target:
return True
else:
return False
present = contains([4, 8, 12], 12)
absent = contains([4, 8, 12], 7)
Why did the original contains function fail when the target appeared after the first element?
Identify exactly when the incorrect return executed and its effect on the loop.
Students type their answer here.
A student says that learning the steps of searching and sorting means memorising one exact Python program. Evaluate this claim for OCR J277.
Distinguish understanding, applying and recognising an algorithm from recalling one implementation.
Students type their answer here.
Block B checkpoint
You should now be able to manipulate strings, store and process array-style data, design reusable subprograms, and apply or recognise OCR's required searching and sorting algorithms. Focus on the data movement and decisions: different valid code can express the same algorithmic method.