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.
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.