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
bubble-sort
Bubble Sort
What is a bubble sort?
-
A bubble sort sorts items into order, smallest to largest
-
It compares pairs of elements and swaps them if they are out of order
-
The first element is compared to the second, the second to the third, the third to the fourth and so on, until the second to last is compared to the last. Swaps occur if each comparison is out of order
-
This overall process is called a pass
Examiner Tips and Tricks
The highest value in the list eventually “bubbles” its way to the top like a fizzy drink, hence the name “Bubble sort”
-
Once the end of the list has been reached, the value at the top of the list is now in order and the sort resets back to the start of the list. The next largest value is then sorted to the top of the list
-
More passes are completed until all elements are in the correct order
-
A final pass checks all elements and if no swaps are made then the sort is complete
-
An example of using a bubble sort would be sorting an array of names into alphabetical order, or sorting an array of student marks from a test
Performing the bubble sort

Figure 1: Performing the Bubble sort
Time complexity of the bubble 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
-
Four statements are present in the above algorithm O(1), followed by a while loop O(n) containing two statements and a further for loop O(n). This results in the expression: 2n2 + 4
-
As coefficients and lower order values are not used, this give the bubble sort O(n2) worst case time complexity
-
The best case scenario would be an already sorted list which means a time complexity of O(n) as each item must still be compared to check if they are in order
-
The average case scenario would be an almost sorted list leading to O(n2/2), which if coefficients are removed still gives O(n2)
Space complexity of the bubble sort
-
A bubble sort has a space complexity of O(1) because it operates in-place, requiring a fixed amount of memory
-
The fixed amount of memory is for variables such as loop counters and a temporary swap variable
Tracing a bubble sort
-
The trace table follows the algorithm through, updating values that change in the table
-
Each iteration of the list reduces the overall size of the list by 1 as after each pass the previously sorted final digit is in order and does not need to be rechecked. This means that 9 is in order and doesn’t need to be rechecked, followed by 9 and 7, followed by 9, 7 and 6 and so on
-
When the if statement is checked a new row has been added to show the swap of list[j], list[j + 1] and Temp, followed by the subsequent change to Swap from FALSE to TRUE
-
After several iterations (that are not shown) the algorithm will eventually output a sorted list
Trace table of the bubble sort
|
LastElement |
Index |
Mylist[Index] |
Mylist[Index + 1] |
Temp |
Swap |
Output |
|---|---|---|---|---|---|---|
|
10 |
1 |
5 |
9 |
|
FALSE |
|
|
|
2 |
9 |
4 |
|
|
|
|
|
|
4 |
9 |
4 |
TRUE |
|
|
|
3 |
9 |
2 |
|
|
|
|
|
|
2 |
9 |
9 |
|
|
|
|
4 |
9 |
6 |
|
|
|
|
|
|
6 |
9 |
|
|
|
|
|
5 |
9 |
7 |
|
|
|
|
|
|
7 |
9 |
|
|
|
|
|
6 |
9 |
1 |
|
|
|
|
|
|
1 |
9 |
|
|
|
|
|
7 |
9 |
2 |
|
|
|
|
|
|
2 |
9 |
|
|
|
|
|
8 |
4 |
9 |
|
|
|
|
|
|
9 |
4 |
|
|
|
|
|
9 |
9 |
3 |
|
|
|
|
|
|
3 |
9 |
|
|
|
|
9 |
1 |
5 |
4 |
|
FALSE |
|
|
|
|
4 |
5 |
5 |
TRUE |
|
|
|
2 |
5 |
2 |
|
|
|
|
|
|
2 |
5 |
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
|
|
|
FALSE |
1, 2, 2, 3, 4, 4, 5, 6, 7, 9 |
Implementing a Bubble Sort
Pseudocode
list = [5, 9, 4, 2, 6, 7, 1, 2, 4, 3]
last = len(list)
swap = True
i = 0
while i < (last - 1) and (swap = True) swap = False for j = 0 to last - i - 2 if list[j] > list[j+1] temp = list[j] list[j] = list[j+1] list[j+1] = temp swap = True endif next j i = i + 1
endwhile
print(list)
Responses