You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Sorting algorithms arrange data into a specific order — usually ascending (smallest to largest) or descending (largest to smallest). In GCSE Computer Science, you must understand three sorting algorithms: bubble sort, merge sort, and insertion sort. You need to know how each works, trace through examples, and compare their efficiency.
Bubble sort works by repeatedly comparing adjacent pairs of items and swapping them if they are in the wrong order. The largest unsorted value "bubbles" to the end of the list after each pass. The algorithm repeats until no swaps are made in a complete pass.
PROCEDURE bubbleSort(list)
n ← LENGTH(list)
FOR i ← 0 TO n - 2
swapped ← FALSE
FOR j ← 0 TO n - 2 - i
IF list[j] > list[j + 1] THEN
temp ← list[j]
list[j] ← list[j + 1]
list[j + 1] ← temp
swapped ← TRUE
END IF
END FOR
IF swapped = FALSE THEN
RETURN // List is sorted, exit early
END IF
END FOR
END PROCEDURE
List: [5, 3, 8, 1, 2]
Pass 1:
Pass 2:
Pass 3:
Pass 4:
Merge sort uses a divide-and-conquer approach. It works by repeatedly splitting the list in half until each sub-list contains a single item, then merging the sub-lists back together in the correct order.
When merging two sorted sub-lists:
List: [6, 3, 8, 1, 5, 2]
Split phase:
[6, 3, 8, 1, 5, 2]
/ \
[6, 3, 8] [1, 5, 2]
/ \ / \
[6] [3, 8] [1] [5, 2]
/ \ / \
[3] [8] [5] [2]
Merge phase:
[3] [8] [5] [2]
\ / \ /
[3, 8] [2, 5]
\ / \ /
[3, 6, 8] [1, 2, 5]
\ /
[1, 2, 3, 5, 6, 8]
FUNCTION mergeSort(list)
IF LENGTH(list) <= 1 THEN
RETURN list
END IF
mid ← LENGTH(list) DIV 2
left ← mergeSort(list[0..mid-1])
right ← mergeSort(list[mid..end])
RETURN merge(left, right)
END FUNCTION
FUNCTION merge(left, right)
result ← []
WHILE LENGTH(left) > 0 AND LENGTH(right) > 0
IF left[0] <= right[0] THEN
APPEND left[0] TO result
REMOVE first item from left
ELSE
APPEND right[0] TO result
REMOVE first item from right
END IF
END WHILE
APPEND remaining items from left TO result
APPEND remaining items from right TO result
RETURN result
END FUNCTION
Insertion sort works by building a sorted section of the list one item at a time. It takes each item from the unsorted section and inserts it into the correct position in the sorted section.
PROCEDURE insertionSort(list)
FOR i ← 1 TO LENGTH(list) - 1
key ← list[i]
j ← i - 1
WHILE j >= 0 AND list[j] > key
list[j + 1] ← list[j]
j ← j - 1
END WHILE
list[j + 1] ← key
END FOR
END PROCEDURE
List: [5, 3, 8, 1, 2]
| Pass | Key | Action | List After |
|---|---|---|---|
| 1 | 3 | 3 < 5, insert before 5 | [3, 5, 8, 1, 2] |
| 2 | 8 | 8 > 5, stays in place | [3, 5, 8, 1, 2] |
| 3 | 1 | 1 < 8, 1 < 5, 1 < 3, insert at start | [1, 3, 5, 8, 2] |
| 4 | 2 | 2 < 8, 2 < 5, 2 < 3, 2 > 1, insert after 1 | [1, 2, 3, 5, 8] |
| Feature | Bubble Sort | Merge Sort | Insertion Sort |
|---|---|---|---|
| Best case | O(n) — already sorted | O(n log n) | O(n) — already sorted |
| Worst case | O(n²) | O(n log n) | O(n²) |
| Average case | O(n²) | O(n log n) | O(n²) |
| In-place? | Yes | No (needs extra memory) | Yes |
| Stable? | Yes | Yes | Yes |
| Best for | Small/nearly sorted lists | Large datasets | Small/nearly sorted lists |
Exam Tip: Be ready to trace through each algorithm step by step. Merge sort questions often ask you to show the split and merge phases. Bubble sort questions often ask how many passes or swaps are needed. Insertion sort questions ask you to show the sorted section growing.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.