Merge Sort

Learn how the Merge Sort algorithm organizes data using a divide and conquer strategy.

Warm Up: Divide and Conquer

Status: SPLITTING

The list must be divided. Click any sublist with multiple cards to split it in half.

What is Merge Sort?

The merge sort algorithm has two parts: splitting items and merging items. It starts by splitting a list into halves called sublists, repeating until each sublist contains only single items.

The merge sort algorithm is an example of a divide and conquer approach. It breaks down a problem into smaller parts until they are simple enough to solve directly.

  • Splitting: Repeatedly dividing the group into half until you have separate groups with a single item.
  • Merging: Reassembling groups by comparing the first items of two sublists and placing the lowest value into a new ordered list.

Numerical Example: Olympic Medals

Sorting medal counts: [15, 25, 13, 29, 18].

1. Visualising the Split & Merge

Ready

MERGE SORT

Click start to begin the visualization

15
25
13
29
18

Try it yourself!

The list [10, 12, 6, 9, 5, 6, 5] has been split once. How will the sublists look after the second split?

[10, 12, 6, 9]
[5, 6, 5]

Sorting Words: Computer Scientists

Sorting famous pioneers alphabetically: ["Ada Lovelace", "Grace Hopper", "Anita Borg", "Margaret Hamilton", "Annie Easley", "Joan Clarke", "Mary Wilkes", "Karen Spärck Jones"].

2. Alphabetical Merge Sort

Ready

MERGE SORT

Click start to begin the visualization

Ada Lovelace
Grace Hopper
Anita Borg
Margaret Hamilton
Annie Easley
Joan Clarke
Mary Wilkes
Karen Spärck Jones

Algorithm Implementation

The instructions for merge sort involve a recursive logic of splitting and merging.

Merge Sort Pseudocode

1
FUNCTION merge_sort(items)
2
IF LEN(items) <= 1 THEN RETURN items
3
mid = LEN(items) // 2
4
left = merge_sort(items[:mid])
5
right = merge_sort(items[mid:])
6
RETURN merge(left, right)
7
8
FUNCTION merge(left, right)
9
result = []
10
WHILE left AND right NOT empty:
11
IF left[0] <= right[0]:
12
APPEND left.POP(0) TO result
13
ELSE:
14
APPEND right.POP(0) TO result
15
APPEND remaining TO result
16
RETURN result

Sublists State

Status

Idle

Ready to trace...

def merge_sort(items):
    if len(items) <= 1:
        return items
    
    mid = len(items) // 2
    left = merge_sort(items[:mid])
    right = merge_sort(items[mid:])
    
    return merge(left, right)

def merge(left, right):
    result = []
    while left and right:
        if left[0] <= right[0]:
            result.append(left.pop(0))
        else:
            result.append(right.pop(0))
    result.extend(left if left else right)
    return result

# Merge sort is highly efficient for large datasets because it uses a divide and conquer strategy, reducing the total number of comparisons.

Efficiency Scenarios

Unlike Bubble Sort, Merge Sort always splits the list regardless of its initial order.

Best Case

Smallest number of comparisons during merging.

Ready

MERGE SORT

Click start to begin the visualization

1
2
3
4
5

Worst Case

Every item must be compared at each stage.

Ready

MERGE SORT

Click start to begin the visualization

5
1
4
2
3

Fill the Gaps

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Order the Steps

Arrange the steps of the Merge Sort algorithm.

Available Steps:

Your sequence:

Click items above to build your sequence

Keywords Memory Game

A-Level Only: Efficiency of Merge Sort

Let's analyse the Time and Space complexity of Merge Sort using Big O notation. The graph below highlights why Merge Sort is so powerful: its Best, Average, and Worst Case Time Complexity are all Linearithmic O(n log n). However, this speed comes at the cost of requiring more memory, leading to a Linear Space Complexity of O(n).

Fill the Gaps

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)