Loading…
Loading…
Loading…
AQA GCSE Computer Science · 8525
AQA 8525 Check the specification (PDF) (opens in a new tab)
A sorting algorithm gives a computer precise instructions for putting data into a chosen order. For example, numbers can be sorted into , from smallest to largest.
Merge sort uses a divide-and-conquer strategy. It breaks a list into smaller sublists until each contains just one value, then combines them into increasingly larger sorted lists. A one-value list is already sorted: there is nothing within it that could be in the wrong order.
The important connection is that merging two sorted sublists produces another sorted sublist. Repeating this process eventually produces one sorted list containing all the original values.
Consider the list [7, 4, 1, 2, 6, 3, 8, 5].
First, split it into two halves: [7, 4, 1, 2] and [6, 3, 8, 5]. Split each half again, giving [7, 4], [1, 2], [6, 3] and [8, 5]. Finally, split these into eight one-value sublists.
During this stage, the values keep their original order. Splitting makes the sorting problem smaller; it does not yet put the values into order. If a sublist has an odd number of values, its two parts can differ in size by one. Continue splitting until each sublist has one value.
Repeated splitting creates one-value sublists; repeated merging rebuilds the list in ascending order.
Now merge pairs of sublists. For ascending order, select the smaller value first. For example, [7] and [4] become [4, 7].
After merging all the one-value sublists in pairs, the four sorted sublists are [4, 7], [1, 2], [3, 6] and [5, 8].
When merging longer sublists, compare their first unused values. Take the smaller value and add it to the end of the new list. Move forward in the sublist you took it from, then compare again.
Here is how [4, 7] and [1, 2] merge:
| Values compared | Action | New list so far |
|---|---|---|
| 4 and 1 | Take 1 from the second sublist | [1] |
| 4 and 2 | Take 2 from the second sublist | [1, 2] |
| Second sublist exhausted | Append the remaining 4 and 7 | [1, 2, 4, 7] |
Appending the remaining values works because that sublist is already sorted. Likewise, merging [3, 6] and [5, 8] produces [3, 5, 6, 8].
If the compared values are equal, take either one first, but keep both values in the result. Sorting changes their order, not how many values there are.
The last two sublists are now [1, 2, 4, 7] and [3, 5, 6, 8].
Compare 1 with 3 and take 1. Next, compare 2 with 3 and take 2. Then compare 4 with 3 and take 3. Notice that 4 stays available for the next comparison: only the sublist supplying the chosen value moves forward.
Continue selecting the smaller first unused value. This takes 4, then 5, then 6, then 7. The first sublist is now exhausted, so append the remaining 8.
The result is [1, 2, 3, 4, 5, 6, 7, 8]. Merge sort is complete when all the sublists have been combined into one sorted list.
Divide and conquer: reduce the problem to smaller parts, then combine their solutions. Every merge produces a sorted sublist, so the final merged list is sorted.
Show the intermediate sublists when tracing merge sort, not just the final sorted list.
Keep the original order of the values during splitting: sorting happens when the sublists are merged.
During a merge, compare the first unused value in each sorted sublist. Advance only in the sublist whose value you have taken.
When one sublist is exhausted, append all remaining values from the other sublist in their existing order.
It can see this note and what your specification asks for.
Answers are free with an account.
1 free note left this week
Get more free notesYour answer sets when this note comes back for review, or marks it complete.
Was this note helpful?
Log in to tell us whether it helped, or to report a problem.
Last updated
Sorting algorithm
A precise sequence of instructions for arranging data into a specified order, such as numbers from smallest to largest.
Merge sort
A sorting algorithm that repeatedly splits a list into smaller sublists, then merges those sublists back together in order.
Divide and conquer
A strategy that breaks a problem into smaller problems, solves them and combines their solutions.
Sublist
A smaller list formed from part of a larger list.
Merge
Combining two sorted lists into one sorted list by repeatedly selecting the next value in the required order.
Put your knowledge into practice — try past paper questions for Computer Science
Sorting algorithm
A precise sequence of instructions for arranging data into a specified order, such as numbers from smallest to largest.
Merge sort
A sorting algorithm that repeatedly splits a list into smaller sublists, then merges those sublists back together in order.
Divide and conquer
A strategy that breaks a problem into smaller problems, solves them and combines their solutions.
Sublist
A smaller list formed from part of a larger list.
Merge
Combining two sorted lists into one sorted list by repeatedly selecting the next value in the required order.