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 主题
insertion-sort- as
Exam code:H046
Insertion sort
What is an insertion sort?
-
The insertion sort sorts one item at a time by placing it in the correct position of an unsorted list
-
This process repeats until all items are in the correct position
-
Specifically, the current item being sorted is compared to each previous item
-
If it is smaller than the previous item then the previous item is moved to the right and the current item takes its place
-
If the current item is larger than the previous item then it is in the correct position and the next current item is then sorted as illustrated below
Performing the insertion sort

Figure 2: Performing the Insertion sort
Time complexity of an insertion sort
-
In the above algorithm, one statement is present, a for loop with three statements and a nested while loop that contains two statements
-
The time complexity for the insertion sort would be 3n*2n + 1, giving 6n2 + 1. 3n comes from the for loop having three statements inside it, not including the while. 2n comes from the while loop having two statements inside it
-
Removing coefficients and dominated factors leaves O(n2) as the worst case scenario
-
The best case scenario would be an already sorted list which means each item is checked one at a time giving a time complexity of O(n)
The average case scenario would be a half sorted list giving O(n2/2), which when coefficients are removed still gives O(n2)
Space complexity of an insertion sort
-
As the insertion sort requires no additional space, it is space complexity O(1)
Tracing an insertion sort
-
Tracing through the insertion sort involves starting at element 1 (not 0)
-
i increments through each element one at a time and each item is compared to the one previous. If the previous item is bigger or equal it is set to list[position]
-
If position equals 0 then the iteration is at the start of the list and the next iteration begins
Trace Table of the Insertion sort
|
list |
i |
item |
position |
list[position-1] |
list[position] |
|---|---|---|---|---|---|
|
[5, 9, 4, 2, 7, 1, 2, 4, 3] |
1 |
9 |
1 |
5 |
9 |
|
|
2 |
4 |
2 |
9 |
9 |
|
|
|
|
1 |
5 |
5 |
|
|
|
|
0 |
|
|
|
[4, 5, 9, 2, 7, 1, 2, 4, 3] |
3 |
2 |
3 |
9 |
9 |
|
|
|
|
2 |
5 |
5 |
|
|
|
|
1 |
4 |
4 |
|
|
|
|
0 |
|
|
|
[2, 4, 5, 9, 7, 1, 2, 4, 3] |
4 |
7 |
4 |
9 |
9 |
|
|
|
|
3 |
5 |
7 |
|
[2, 4, 5, 7, 9, 1, 2, 4, 3] |
5 |
1 |
5 |
9 |
9 |
|
|
|
|
4 |
7 |
7 |
|
|
|
|
3 |
5 |
5 |
|
|
|
|
2 |
4 |
4 |
|
|
|
|
1 |
2 |
2 |
|
|
|
|
0 |
|
1 |
|
[1, 2, 4, 5, 7, 9, 2, 4, 3] |
6 |
2 |
6 |
9 |
9 |
|
|
|
|
5 |
7 |
7 |
|
|
|
|
4 |
5 |
5 |
|
|
|
|
3 |
4 |
4 |
|
|
|
|
2 |
2 |
2 |
|
[1, 2, 2, 4, 5, 7, 9, 4, 3] |
7 |
4 |
7 |
9 |
9 |
|
|
|
|
6 |
7 |
7 |
|
|
|
|
5 |
5 |
5 |
|
|
|
|
4 |
4 |
4 |
|
[1, 2, 2, 4, 4, 5, 7, 9, 3] |
8 |
3 |
8 |
9 |
9 |
|
|
|
|
7 |
7 |
7 |
|
|
|
|
6 |
5 |
5 |
|
|
|
|
5 |
4 |
4 |
|
|
|
|
4 |
4 |
4 |
|
|
Responses