Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

Bubble sort

What is a bubble sort?

  • In A Level Computer Science, 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

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

 

 

 

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注