Two more sorting algorithms for GCSE OCR Computer Science: Insertion Sort and Merge Sort — with animated step-by-step walkthroughs, a step-ordering drag-and-drop, and a timed 12-question quiz.
The essentials for each algorithm, in your own words.
Insertion sort builds up a sorted list one item at a time. It takes the next item — the key — and slides it backwards past any bigger items already in the sorted part of the list, until it reaches its correct place.
Merge sort is a divide and conquer algorithm. It splits the list in half, again and again, until every group has just one item — then merges those groups back together in the correct order.
Watch each item get compared with the sorted items before it, then slotted into its correct place.
FOR i FROM 1 TO length of list - 1
SET key TO list[i]
SET j TO i - 1
WHILE j >= 0 AND list[j] > key DO
SET list[j + 1] TO list[j]
SET j TO j - 1
END WHILE
SET list[j + 1] TO key
END FOR
Watch the list split all the way down to single items, then merge back together in sorted order.
MergeSort(list):
IF length of list <= 1 THEN
RETURN list
END IF
SET mid TO length of list DIV 2
SET left TO MergeSort(first half of list)
SET right TO MergeSort(second half of list)
RETURN Merge(left, right)
Merge(left, right):
SET result TO empty list
WHILE left is not empty AND right is not empty DO
IF first item of left <= first item of right THEN
APPEND first item of left TO result
REMOVE first item from left
ELSE
APPEND first item of right TO result
REMOVE first item from right
END IF
END WHILE
APPEND any remaining items from left TO result
APPEND any remaining items from right TO result
RETURN result
Drag the steps (or click one, then click another to swap them) into the correct order for each algorithm, then check your answer.
OCR-style multiple choice questions on insertion sort and merge sort. A timer starts as soon as you press Start.