An insertion sort is one type of sorting algorithm.
A student has written a pseudocode algorithm to perform an insertion sort on a 1D array of strings cities.
cities = ["Tokyo", "Paris", "Cairo", "Berlin", "Sydney"]
for count = 1 to cities.length - 1
idx = count
while (idx > 0 and cities[idx] < cities[idx - 1])
temp = cities[idx]
cities[idx] = cities[idx - 1]
cities[idx - 1] = temp
idx = idx - 1
endwhile
next count
An insertion sort contains nested loops. In this pseudocode algorithm, the outer loop is a count-controlled loop and the inner loop is a condition-controlled loop.
Explain why the inner loop needs to be a condition-controlled loop.
Practise OCR GCSE Computer Science Searching and sorting algorithms with exam-style questions for GCSE Computer Science. 36 questions, matched to the OCR GCSE Computer Science (J277) specification and written in Component 01 and Component 02 style. Every question includes a full worked solution and mark scheme, so you can see where marks are awarded rather than just whether you got the answer right.