Computer Science AS OCR
-
1-1-structure-and-function-of-the-processor as5 主题
-
1-2-types-of-processor as3 主题
-
1-3-input-output-and-storage as2 主题
-
2-1-systems-software as8 主题
-
2-3-software-development as5 主题
-
2-4-types-of-programming-language as4 主题
-
3-1-compression-encryption-and-hashing as3 主题
-
3-2-databases as3 主题
-
3-3-networks as8 主题
-
3-4-web-technologies as13 主题
-
html as
-
css as
-
css-styling as
-
javascript as
-
variables-and-constants-in-javascript as
-
outputs-in-javascript as
-
selection-in-javascript- as
-
for-loops-in-javascript- as
-
while-loops-in-javascript- as
-
strings-in-javascript- as
-
operators-in-javascript- as
-
nested-statements-in-javascript as
-
functions-and-procedures-in-javascript as
-
html as
-
4-1-data-types as8 主题
-
4-2-data-structures as4 主题
-
4-3-boolean-algebra as1 主题
-
5-1-computing-related-legislation as4 主题
-
5-2-moral-and-ethical-issues as9 主题
-
6-1-thinking-abstractly as3 主题
-
6-2-thinking-ahead as1 主题
-
6-3-thinking-procedurally as3 主题
-
6-4-thinking-logically as2 主题
-
6-5-thinking-concurrently as2 主题
-
7-1-programming-techniques as9 主题
-
8-1-standard-algorithms-and-big-o-notation as8 主题
merge-sort- as
Exam code:H046
Merge sort
What is a merge sort?
-
Merge sort is an example of a divide and conquer algorithm that reduces the size of the problem into smaller problems that are more easily solvable by an algorithm
-
Specifically, the merge sort divides the list in half, creating two sub lists. Each sub list is then divided into two until each sub list contains only one item or element
-
Groups of two sub lists are then sorted and merged together to form new, larger sub lists until all sub lists have been merged into a single sorted list
-
The merge sort therefore contains two parts:
-
Part one: Divide the list into sub lists until each sub list contains only one element
-
Part two: Repeatedly merge groups of two and sort them, until all lists have been merged into a single sorted list
-
-
The merge sort has been illustrated below using a list of ten items: [15, 5, 2, 7, 4, 9, 10, 1, 8, 3]
Performing the merge sort

Figure 1: Performing the Merge sort
-
Notice that halving the list sometimes produces odd numbers of elements in sub lists. When dividing, all sub lists must contain a single element before merging can begin.
-
When merging, only two sub lists can be merged at a time. Any left over sub lists are merged in the next merging iteration
-
In order to merge two sub lists, two pointers are required, one for each sub list. The pointer keeps track of which elements are to be compared. Once an element has been merged into the new list, the corresponding pointer is incremented. The process continues until a list contains no elements to compare, at which point the remaining elements are appended to the end of the new merged sub list.
Time complexity of a merge sort
-
To determine the algorithm’s execution time as measured by the number of instructions or steps carried out Big-O notation must be used
-
The time complexity of a merge sort is dependent on its divide and conquer nature O(logn) and the fact that n sub lists must be merged. This means the overall worst case time complexity of the merge sort is O(nlogn)
-
The best case and average case scenario time complexity of the merge sort is O(nlogn) as regardless of the number of elements or item, n must still be merged and halved
Space complexity of a merge sort
-
The merge sort requires twice the amount of space of other sorting algorithms due to holding copies of the left and right halves of the list
-
Its space complexity is therefore O(n) original space and an additional O(n) space to store the copies of the list
Tracing a merge sort
-
In the trace table below, several calls to merge sort are made which splits the list. Each call generates a stack frame which is put on the stack. Stack frames are removed when the current stack frame reaches the end of its code
-
The list is gradually broken down into sub lists, starting with the left side of each sub list. Sub lists are created until each sub list contains only one element which is the base case. When a base case is reached the stack frame is removed and the program continues execution on the line after the call to merge sort
-
Each sub list is gradually merged, two at a time and passed up the stack to be merged with another sub list in a later merge sort call
-
Pointers are incremented during the merge such that the next element to merge in each sub list is tracked and appropriately added to the new list. NewListPointer tracks the position of elements in the new list
-
Row 25 shows the algorithm completing the left side of the list and the beginning of the merge of the right sub list which repeats the process outlined in the trace table
Trace table for the merge sort
|
Row |
Stack frame |
len(list) > 1? |
left |
right |
Split left, right or merge? |
newList |
mid |
Left Pointer |
Right Pointer |
newListPointer |
|---|---|---|---|---|---|---|---|---|---|---|
|
1 |
1 |
TRUE |
15, 5, 2, 7, 4 |
9, 10, 1, 8, 3 |
LEFT |
|
5 |
|
|
|
|
2 |
2 |
TRUE |
15, 5 |
2, 7, 4 |
LEFT |
|
2 |
|
|
|
|
3 |
3 |
TRUE |
15 |
5 |
LEFT |
|
0 |
|
|
|
|
4 |
4 |
FALSE |
|
|
BASE CASE |
|
|
|
|
|
|
5 |
3 |
TRUE |
15 |
5 |
RIGHT |
|
0 |
|
|
|
|
6 |
4 |
FALSE |
|
|
BASE CASE |
|
|
|
|
|
|
7 |
3 |
TRUE |
15 |
5 |
MERGE |
[5, 15] |
0 |
0 |
0 |
0 |
|
8 |
3 |
TRUE |
2 |
7, 4 |
LEFT |
|
1 |
|
|
|
|
9 |
4 |
FALSE |
|
|
BASE CASE |
|
|
|
|
|
|
10 |
3 |
TRUE |
7 |
4 |
LEFT |
|
0 |
|
|
|
|
11 |
4 |
FALSE |
|
|
BASE CASE |
|
|
|
|
|
|
12 |
3 |
TRUE |
7 |
4 |
RIGHT |
|
0 |
|
|
|
|
13 |
4 |
FALSE |
|
|
BASE CASE |
|
|
|
|
|
|
14 |
3 |
TRUE |
7 |
4 |
MERGE |
[4, 7] |
0 |
0 |
0 |
0 |
|
15 |
3 |
TRUE |
2 |
4, 7 |
MERGE |
|
1 |
0 |
Responses