An insertion sort is a common sorting algorithm.
A seismologist designs a pseudocode algorithm to sort an array of earthquake magnitudes, magnitudes, into descending order.
magnitudes = [5.2, 6.8, 4.1, 7.3, 5.9]
for p = 1 to magnitudes.length - 1
current_pos = p
while (current_pos > 0 and magnitudes[current_pos] > magnitudes[current_pos - 1])
temp = magnitudes[current_pos]
magnitudes[current_pos] = magnitudes[current_pos - 1]
magnitudes[current_pos - 1] = temp
current_pos = current_pos - 1
endwhile
next p
Describe the purpose of the variable temp in this algorithm.
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.