0% found this document useful (0 votes)
6 views9 pages

Bubble Sort Algorithm

Bubble sort

Uploaded by

lrogers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Bubble Sort Algorithm

Bubble sort

Uploaded by

lrogers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Bubble Sort Algorithm

Sort Algorithms

Sorting algorithms are used to sort data in particular ways (for example sorting a set of
numbers from highest to lowest). There are some standard sorting algorithms that have
been developed and each have their own merits. Over the next few pages we will take a
look at the ‘Bubble Sort’ algorithm.

The Bubble Sort Algorithm

A bubble sort is quite simple in the way it works. It repeatedly steps through a list of data,
comparing each pair of data it comes across and swaps them if their order is wrong.

An Example:

Computer Science UK Membership Site Licence: Do not share outside your centre.
Computer Science UK Membership Site Licence: Do not share outside your centre.
It is important to recognise that although the above dataset is now in order, there will be
a third and final pass carried out on the dataset. This is because algorithms are not
intelligent, they cannot see that the dataset has been sorted until a pass is carried out in
which no swaps are made.

Bubble Sort Algorithm Pseudocode Example:

Questions (The question zone you choose must either match your target grade or be higher!)

Question Zone 1-3

1. What is the purpose of the ‘Bubble Sort Algorithm’?. [1]


to sort à list of values from lowest to highest
2. What does the term ‘pass’ mean, when discussing ‘Bubble Sort Algorithms’? [3]
when the computer gos through the list of numbers once, thats described as à pass
3. Describe how the ‘Bubble Sort’ works. [3]

Computer Science UK Membership Site Licence: Do not share outside your centre.
bubble sort works by repeatedly going through the list of values and checking two
of them next twoo each other and sorting them to make sure theyre lower to
higher. every time it goes through the list once, its called à pass. once all the
numbers are sorted, it will go through one last time to check.

Question Zone 4-6

1. Describe how the ‘Bubble Sort’ works. [3] DONE


2. For a given dataset, a ‘Bubble Sort Algorithm’ takes 4 passes to sort a set of data.
Will the algorithm stop at this point? Explain your answer. [3]
no, because it will go through one last time to ensure its all in order, just in case
there was à number that was missed, which is time-consuming
3. Show the steps that will take place to sort the following dataset, during a ‘Bubble
Sort’. [5] DONE BELOW

Questions Zone 7-9

1. Show the steps that will take place to sort the following dataset, during a ‘Bubble
Sort’. [5]
[10, 5, 20, 15, 0]
[5, 10, 20, 15, 0]
[5, 10, 20, 15, 0]
[5, 10, 15, 20, 0]
[5, 10, 15, 0, 20]

[5, 10, 0, 15, 20]

[5,0,10,15,20]
[0,5,10,15,20]
[0,5,10,15,20]

2. How many comparisons and swaps are required to sort the following set of data
using a ‘Bubble Sort’? You must show your working out. [4]
[2, 5, 4, 8, 7, 6]
[2,5,4,8,7,6]
[2,4,5,8,7,6]
[2,4,5,8,7,6]
[2,4,5,7,8,6]
[2,4,5,7,6,8]

[2,4,5,7,6,8]

Computer Science UK Membership Site Licence: Do not share outside your centre.
[2,4,5,7,6,8]
[2,4,5,7,6,8]
[2,4,5,6,7,8]
3 passes, 14 comparisons
3. What is the maximum number of comparisons and swaps that a ‘Bubble Sort
Algorithm’ could make if there are 5 items of data in a dataset? You must show
your working out. [5]

5,4,3,2,1

4,5,3,2,1

4,3,5,2,1

4,3,2,5,1

4,3,2,1,5

3,4,2,1,5

3,2,4,1,5

3,2,1,4,5

3,2,1,4,5

2,3,1,4,5

2,1,3,4,5,

2,1,3,4,5

2,1,3,4,5

1,2,3,4,5

1,2,3,4,5

1,2,3,4,5

1,2,3,4,5

one more go through to check

25 checks

15 swaps

Computer Science UK Membership Site Licence: Do not share outside your centre.
Computer Science UK Membership Site Licence: Do not share outside your centre.
Computer Science UK Membership Site Licence: Do not share outside your centre.
Computer Science UK Membership Site Licence: Do not share outside your centre.
Computer Science UK Membership Site Licence: Do not share outside your centre.

You might also like