Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
insertion-sort-and-bubble-sort
Insertion sort
What is an insertion sort?
-
In A Level Computer Science, 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] |
Responses