Merge Sort (AQA GCSE Computer Science)

Revision Note

James Woodhouse

Expertise

Computer Science

What is a sorting algorithm?

  • Sorting algorithms are precise step-by-step instructions that a computer can follow to efficiently sort data in massive datasets
  • Two common sorting algorithms are:
    • Bubble sort
    • Merge sort

Merge Sort

What is a merge sort?

  • A merge sort is a sorting algorithm that uses the 'divide and conquer' strategy of dividing a dataset into smaller sub-datasets and merging them back together in the correct order

How do you perform a merge sort?

Step Instruction
1 Divide the dataset into individual datasets by repeatedly splitting the dataset in half (DIVIDE)
2 Merge pairs of sub-datasets together by comparing the first value in each dataset (CONQUER)
3 REPEAT step 2 until all sub-datasets are merged together into one dataset

Example

  • Perform a merge sort on the following dataset
7 4 1 2 6 3 8 5

Step Instruction
1

Divide the dataset into individual datasets by repeatedly splitting the dataset in half (DIVIDE)

7 4 1 2 6 3 8 5

divide in half (2 datasets of 4 values)
7 4 1 2   6 3 8 5

divide in half again (4 datasets of 2 values)
7 4   1 2   6 3   8 5

divide in half again  (8 datasets of 1 value)
7   4   1   2   6   3   8   5

2

Merge pairs of sub-datasets together by comparing the first value in each dataset (CONQUER)

7   4   1   2   6   3   8   5

Merge into 4 datasets of 2 values
4 7   1 2   3 6   5 8

3

REPEAT step 2 until all sub-datasets are merged together into one dataset

Merge into 2 datasets of 4 values
1 2 4 7   3 5 6 8

Merge into 1 dataset of 8 values (in the correct order)
1 2 3 4 5 6 7 8

Exam Tip

In the exam, the divide stage could already be done for you and you would only need to demonstrate the conquer stage!

A Merge Sort in Python

# List of numbers to perform merge sort on
numbers = [7, 4, 1, 2, 6, 3, 8, 5]

# Merge function to merge two sorted lists
def merge(left, right):
    merged = []
    left_index, right_index = 0, 0

    # Merge the two sorted lists
    while left_index < len(left) and right_index < len(right):
        if left[left_index] < right[right_index]:
            merged.append(left[left_index])
            left_index += 1
    else:
        merged.append(right[right_index])
        right_index += 1

    # Append remaining elements from left or right sublist i
f there are any remaining elements in the left sublist
    while left_index < len(left):
        merged.append(left[left_index])
        left_index += 1

    # If there are any remaining elements in the right sublist
    while right_index < len(right):
        merged.append(right[right_index])
        right_index += 1
   
    return merged

# Merge sort implementation without using a separate function
def merge_sort(arr):

    # Checks to see if the list has 1 or 0 elements, it's already sorted
    if len(arr) <= 1:
        return arr

    # Split the list into two halves
    mid = len(arr) // 2
    left_half = merge_sort(arr[:mid])  # Split and recursively sort left half
    right_half = merge_sort(arr[mid:])  # Split and recursively sort right half

    # Merge the sorted halves
    return merge(left_half, right_half)

# Perform merge sort
sorted_numbers = merge_sort(numbers)
print("Sorted numbers:", sorted_numbers)

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

James Woodhouse

Author: James Woodhouse

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.