Skip to content

Course home

Searching and sorting algorithms

Searching and sorting algorithms

EasyMedium
1234567891011121314151617
Question 15

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.

[2]
Markscheme

Searching and sorting algorithms Questions

  1. GCSE
  2. /Computer Science
  3. /Searching and sorting algorithms

36 exam-style questions on OCR GCSE Computer Science Searching and sorting algorithms. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank