Bubble sort repeatedly steps through the list, comparing adjacent elements and swapping them if they're in the wrong order. After each pass, the largest unsorted element "bubbles up" to its correct position at the end.
function bubbleSort(arr)
n ← len(arr)
for i ← 0 to n - 2 // n-1 passes
swapped ← false
for j ← 0 to n - 2 - i // inner loop shrinks each pass
if arr[j] > arr[j+1] then
swap arr[j] and arr[j+1]
swapped ← true
endif
next j
if swapped = false then break // early exit if sorted
next i
return arr
endfunction
Sort: [5, 3, 8, 1, 4]
| Pass | Array state | Swaps |
|---|---|---|
| Pass 1 | [3, 5, 1, 4, 8] | 5↔3, 5↔1, 5↔4 → 8 placed |
| Pass 2 | [3, 1, 4, 5, 8] | 5↔1, 5↔4 → 5 placed |
| Pass 3 | [1, 3, 4, 5, 8] | 3↔1 → 4 placed |
| Pass 4 | [1, 3, 4, 5, 8] | No swaps → early exit |
| Case | Comparisons | Time | Space |
|---|---|---|---|
| Best (already sorted) | n-1 (1 pass) | O(n) | O(1) |
| Worst (reverse sorted) | n(n-1)/2 | O(n²) | O(1) |
| Average | n(n-1)/4 | O(n²) | O(1) |
Stable: Yes — equal elements maintain relative order (swaps only when strictly greater). In-place: Yes — O(1) extra space.
Insertion sort builds a sorted portion one element at a time. It takes each element from the unsorted portion and inserts it into its correct position in the sorted portion — like sorting a hand of playing cards.
function insertionSort(arr)
for i ← 1 to len(arr) - 1
key ← arr[i] // element to insert
j ← i - 1
while j >= 0 AND arr[j] > key
arr[j+1] ← arr[j] // shift right to make space
j ← j - 1
endwhile
arr[j+1] ← key // insert key in correct position
next i
return arr
endfunction
Sort: [5, 2, 4, 6, 1, 3] — sorted portion shown in bold
| Pass | key | Array after insertion |
|---|---|---|
| i=1 | 2 | [2, 5, 4, 6, 1, 3] |
| i=2 | 4 | [2, 4, 5, 6, 1, 3] |
| i=3 | 6 | [2, 4, 5, 6, 1, 3] |
| i=4 | 1 | [1, 2, 4, 5, 6, 3] |
| i=5 | 3 | [1, 2, 3, 4, 5, 6] |
| Case | Comparisons | Time | Space |
|---|---|---|---|
| Best (already sorted) | n-1 (one comparison per element) | O(n) | O(1) |
| Worst (reverse sorted) | n(n-1)/2 | O(n²) | O(1) |
| Average | n(n-1)/4 | O(n²) | O(1) |
Stable: Yes. In-place: Yes — O(1) extra space. Best sorting algorithm for nearly-sorted data and online sorting (receiving elements one at a time).
Merge sort is a divide-and-conquer algorithm. It recursively divides the list in half until single elements remain, then merges the halves back together in sorted order.
function mergeSort(arr)
if len(arr) <= 1 then return arr // base case
mid ← len(arr) DIV 2
left ← mergeSort(arr[0..mid-1]) // recursive call
right ← mergeSort(arr[mid..end]) // recursive call
return merge(left, right)
endfunction
function merge(left, right)
result ← []
i ← 0; j ← 0
while i < len(left) AND j < len(right)
if left[i] <= right[j] then
result.append(left[i]); i ← i + 1
else
result.append(right[j]); j ← j + 1
endif
endwhile
// append remaining elements
while i < len(left): result.append(left[i]); i ← i + 1
while j < len(right): result.append(right[j]); j ← j + 1
return result
endfunction
Sort: [5, 3, 8, 1, 4, 2]
Divide: [5, 3, 8, 1, 4, 2]
[5, 3, 8] [1, 4, 2]
[5,3] [8] [1,4] [2]
[5][3] [1][4]
Conquer (merge up):
[5][3] → [3,5] [1][4] → [1,4]
[3,5][8] → [3,5,8] [1,4][2] → [1,2,4]
[3,5,8] + [1,2,4] → [1,2,3,4,5,8]
| Case | Time | Space |
|---|---|---|
| Best | O(n log n) | O(n) |
| Worst | O(n log n) | O(n) |
| Average | O(n log n) | O(n) |
Stable: Yes (use ≤ in merge comparison). Not in-place: O(n) extra space for temporary arrays during merging. The O(n log n) in ALL cases is merge sort's key advantage.
Dividing in half produces log n levels of recursion (same reasoning as binary search). At each level, the total work done across all merge operations is O(n) (merging n elements total). So total = O(n) × O(log n) = O(n log n).
| Property | Bubble Sort | Insertion Sort | Merge Sort |
|---|---|---|---|
| Best case | O(n) | O(n) | O(n log n) |
| Worst case | O(n²) | O(n²) | O(n log n) |
| Average case | O(n²) | O(n²) | O(n log n) |
| Space (extra) | O(1) in-place | O(1) in-place | O(n) not in-place |
| Stable? | Yes | Yes | Yes |
| Best for | Educational/small data | Nearly sorted, online | Large data, guaranteed O(n log n) |
8 questions · 24 marks · instantly marked
| Term | Definition |
|---|