Computer-Science-A-level-Ocr
-
3-3-networks8 主题
-
3-2-databases7 主题
-
3-1-compression-encryption-and-hashing4 主题
-
2-5-object-oriented-languages7 主题
-
2-4-types-of-programming-language4 主题
-
2-3-software-development5 主题
-
2-2-applications-generation6 主题
-
2-1-systems-software8 主题
-
1-3-input-output-and-storage2 主题
-
1-2-types-of-processor3 主题
-
1-1-structure-and-function-of-the-processor1 主题
-
structuring-your-responses3 主题
-
the-exam-papers2 主题
-
8-2-algorithms-for-the-main-data-structures4 主题
-
8-1-algorithms10 主题
-
7-2-computational-methods11 主题
-
7-1-programming-techniques14 主题
-
capturing-selecting-managing-and-exchanging-data
-
entity-relationship-diagrams
-
data-normalisation
-
relational-databases
-
hashing
-
symmetric-vs-asymmetric-encryption
-
run-length-encoding-and-dictionary-coding
-
lossy-and-lossless-compression
-
polymorphism-oop
-
encapsulation-oop
-
inheritance-oop
-
attributes-oop
-
methods-oop
-
objects-oop
-
capturing-selecting-managing-and-exchanging-data
-
6-5-thinking-concurrently2 主题
-
6-4-thinking-logically2 主题
-
6-3-thinking-procedurally3 主题
-
6-2-thinking-ahead1 主题
-
6-1-thinking-abstractly3 主题
-
5-2-moral-and-ethical-issues9 主题
-
5-1-computing-related-legislation4 主题
-
4-3-boolean-algebra5 主题
-
4-2-data-structures10 主题
-
4-1-data-types9 主题
-
3-4-web-technologies16 主题
-
environmental-effects
-
automated-decision-making
-
computers-in-the-workforce
-
layout-colour-paradigms-and-character-sets
-
piracy-and-offensive-communications
-
analysing-personal-information
-
monitoring-behaviour
-
censorship-and-the-internet
-
artificial-intelligence
-
the-regulation-of-investigatory-powers-act-2000
-
the-copyright-design-and-patents-act-1988
-
the-computer-misuse-act-1990
-
the-data-protection-act-1998
-
adder-circuits
-
flip-flop-circuits
-
simplifying-boolean-algebra
-
environmental-effects
merge-sort
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