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
insertion-sort
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