Back to 课程

Computer Science GCES AQA

0% Complete
0/0 Steps
  1. Representing Algorithms Aqa
    4 主题
  2. Efficiency Of Algorithms Aqa
    1 主题
  3. Searching Algorithms Aqa
    3 主题
  4. Sorting Algorithms Aqa
    3 主题
  5. Data Types Aqa
    1 主题
  6. Programming Concepts Aqa
    5 主题
  7. Arithmetic Relational And Boolean Operations Aqa
    1 主题
  8. Data Structures Aqa
    3 主题
  9. String Manipulation Aqa
    1 主题
  10. Random Number Generation Aqa
    1 主题
  11. Structured Programming Aqa
    2 主题
  12. Robust And Secure Programming Aqa
    4 主题
  13. Number Bases Aqa
    2 主题
  14. Converting Between Number Bases Aqa
    3 主题
  15. Units Of Information Aqa
    9 主题
  16. Hardware And Software Aqa
    4 主题
  17. Boolean Logic Aqa
    3 主题
  18. Programming Languages And Translators Aqa
    2 主题
  19. Cpu Architecture Performance And Embedded Systems Aqa
    4 主题
  20. Memory Aqa
    2 主题
  21. Secondary Storage Aqa
    3 主题
  22. Fundamentals Of Computer Networks Aqa
    8 主题
  23. Fundamentals Of Cyber Security Aqa
    1 主题
  24. Methods Of Preventing Cyber Security Threats Aqa
    1 主题
  25. Relational Databases Aqa
    2 主题
  26. Ethical Legal And Environmental Impacts Aqa
    2 主题
课 Progress
0% Complete

Exam code:8525

Bubble Sort

What is a bubble sort?

  • A bubble sort is a simple sorting algorithm that starts at the beginning of a dataset and checks values in ‘pairs’ and swaps them if they are not in the correct order

  • One full run of comparisons from beginning to end is called a ‘pass‘, a bubble sort may require multiple ‘passes‘ to sort the dataset

  • The algorithm is finished when there are no more swaps to make

How do you perform a bubble sort?

Step

Instruction

1

Compare the first two values in the dataset

2

IF they are in the wrong order…

  • Swap them

3

Compare the next two values

4

REPEAT step 2 & 3 until you reach the end of the dataset (pass 1)

5

IF you have made any swaps…

  • REPEAT from the start (pass 2,3,4…)

6

ELSE you have not made any swaps…

  • STOP! the list is in the correct order

Example

  • Perform a bubble sort on the following dataset

5

2

4

1

6

3

Step

Instruction

1

Compare the first two values in the dataset

5

2

4

1

6

3

2

IF they are in the wrong order…

  • Swap them

2

5

4

1

6

3

3

Compare the next two values

2

5

4

1

6

3

4

REPEAT step 2 & 3 until you reach the end of the dataset

  • 5 & 4 SWAP!

2

4

5

1

6

3

  • 5 & 1 SWAP!

2

4

1

5

6

3

  • 5 & 6 NO SWAP!

2

4

1

5

6

3

  • 6 & 3 SWAP!

2

4

1

5

3

6

  • End of pass 1

5

IF you have made any swaps…

  • REPEAT from the start

  • End of pass 2 (swaps made)

2

1

4

3

5

6

  • End of pass 3 (swaps made)

1

2

3

4

5

6

  • End of pass 4 (no swaps)

1

2

3

4

5

6

6

ELSE you have not made any swaps…

  • STOP! the list is in the correct order

Examiner Tips and Tricks

In the exam you do not have to show every swap that takes place in a bubble sort. You can show the outcome of a bubble sort at the end of each pass. If you have the outcome of each pass correct then a bubble sort has been implemented correctly and all marks will be given!

Worked Example

A program uses a file to store a list of words.

A sample of this data is shown

Milk

Eggs

Bananas

Cheese

Potatoes

Grapes

Show the stages of a bubble sort when applied to data shown [2]

How to answer this question

  • We need to sort the values in to alphabetical order from A-Z

  • You CAN use the first letter of each word to simplify the process

Answer

  • E, B, C, M, G, P (pass 1)

  • B, C, E, G, M, P (pass 2)

A bubble sort in python

# unsorted dataset
nums=[66, 7, 69, 50, 42, 80, 71, 321, 67, 8, 39]

# count the length of the dataset
numlength = len(nums)

# sets a flag to initiate the loop
swaps = True

while swaps == True : 
swaps = False
# loop through the dataset
for y in range(numlength-1) :
# if the first number is bigger than the second number
if nums[y] > nums[y+1] :
# swap the numbers using a temporary variable
temp = nums[y]
nums[y] = nums[y+1]
nums[y+1] = temp
# sets the flag to true
swaps = True

# prints the sorted list
print (nums)

Responses

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