Bubble sort

GCSE Computer Science (OCR) · OCR J277 · Algorithms and programming (Paper 2) › Algorithms › Searching and sorting algorithms

66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 11 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.

Revision notes

A bubble sort goes through a list comparing each pair of neighbouring items and swapping them if they are in the wrong order, repeating passes until no swaps are made. You need to apply it by hand, describe it, recognise its code and compare it with the other sorts.

Key facts

  • Compare adjacent pairs; swap if they are in the wrong order
  • One pass = one run through the list, comparing every adjacent pair
  • After pass 1 (ascending), the largest item is at the end
  • Stop when a pass makes no swaps
  • Swap: temp = a, then a = b, then b = temp
  • Pro: simple; in place (little memory); quick to confirm a list is sorted
  • Con: slow and inefficient for large lists

Notes

How a bubble sort works (ascending order)

  • Compare the first two items. If they are in the wrong order, swap them.
  • Move along one place and compare the next pair (items 2 and 3), and so on to the end of the list. This is one pass.
  • After the first pass, the largest item has 'bubbled' to the end, into its final position.
  • Repeat passes until a pass makes no swaps. The list is then sorted.
  • Each pass can stop one item earlier than the last, because the end of the list is already sorted.

Worked example: 6, 2, 9, 4, 3

diagram
  • Pass 1: swap 6 and 2; 6 and 9 stay; swap 9 and 4; swap 9 and 3. Result: 2, 6, 4, 3, 9.
  • 629436 > 2: swap269436 < 9: no swap269439 > 4: swap264939 > 3: swap26439end of pass 1:9 is in place
    Pass 1: each adjacent pair is compared, and swapped if it is in the wrong order
  • Pass 2: 2 and 6 stay; swap 6 and 4; swap 6 and 3; 6 and 9 stay. Result: 2, 4, 3, 6, 9.
  • Pass 3: swap 4 and 3. Result: 2, 3, 4, 6, 9.
  • Pass 4: no swaps, so the sort stops.

The code

  • A temporary variable is needed to swap two items, otherwise one value is overwritten and lost. A flag records whether any swap happened in the pass.
    swapped = true
    while swapped == true
       swapped = false
       for i = 0 to items.length - 2
          if items[i] > items[i + 1] then
             temp = items[i]
             items[i] = items[i + 1]
             items[i + 1] = temp
             swapped = true
          endif
       next i
    endwhile

Advantages and disadvantages

  • Advantages: simple to understand and code; sorts the list in place, so it needs very little extra memory; on a list that is already sorted it finishes after one pass.
  • Disadvantage: very slow on large lists, because it makes many comparisons and swaps.

How to answer each type of question

Show the list after each pass

2 to 4 marksGrade 5
  1. Work left to right, one adjacent pair at a time.
  2. Write the list out again after every swap if it helps.
  3. Only write the list as it stands at the end of each pass as your answer.
  4. Count the final pass that makes no swaps if the question asks how many passes.

Example. A bubble sort is used to put 8, 3, 7, 1, 5 into ascending order.
(a) Show the list after the first pass and after the second pass. [2]
(b) State how many passes are made in total, including the final pass with no swaps. [1]

Show the model answerHide the model answer
(a) After pass 1: 3, 7, 1, 5, 8 (1). After pass 2: 3, 1, 5, 7, 8
(1).
(b) 4 (1): pass 3 gives 1, 3, 5, 7, 8 and pass 4 makes no swaps.

Describe how a bubble sort works

3 to 4 marksGrade 5
  1. Compare adjacent items (the first two to start with).
  2. Swap them if they are in the wrong order.
  3. Move along the list to the end: one pass.
  4. Repeat passes until there are no swaps.

Example. Describe how a bubble sort puts a list of numbers into ascending order. [4]

Show the model answerHide the model answer
Compare the first two (adjacent) items (1). If the first is larger than the second, swap them (1). Move along one place and repeat for each pair to the end of the list, which completes a pass (1). Repeat the passes until a pass is made with no swaps (1).

Complete the swap in bubble sort code

2 to 3 marksGrade 7
  1. Store one value in a temporary variable first.
  2. Copy the second value into the first position.
  3. Copy the temporary value into the second position.
  4. If asked why, say the first value would otherwise be overwritten and lost.

Example. Complete the code that swaps items[i] and items[i + 1], and explain why a temporary variable is needed.

temp = ..........
items[i] = ..........
items[i + 1] = ..........
[3]

Show the model answerHide the model answer
temp = items[i], items[i] = items[i + 1], items[i + 1] = temp (1 for the first two lines, 1 for the last line).
Without temp, the value in items[i] would be overwritten and lost before it could be copied
(1).

Compare bubble sort with another sort

2 marksGrade 8
  1. Give an advantage of bubble sort: simple, or little extra memory.
  2. Give a disadvantage: slow for large lists.

Example. Give one advantage and one disadvantage of a bubble sort compared with a merge sort. [2]

Show the model answerHide the model answer
Advantage: it sorts in place, so it uses less memory / it is simpler to code
(1).
Disadvantage: it is much slower (less efficient) for large lists
(1).

Don’t lose marks

  • Comparing items that are not next to each other.
  • Stopping after one pass because the list 'looks' sorted: the sort only stops after a pass with no swaps.
  • Writing the list after every single swap when the question asks for the list after each pass.
  • Swapping two items without a temporary variable, so one value is lost.
  • Mixing up bubble sort and insertion sort descriptions.

More tips

Memory tricks

  • Picture bubbles rising: after each pass, the biggest unsorted item has risen to the top (the end of the list).
  • Quick check after pass k: the last k items should be the k largest, in order.
  • A list of n items needs at most n − 1 passes that make swaps, plus one final pass with no swaps.

Exam technique

  • Show your working lightly beside the answer box; write each pass result clearly on its own line.
  • When describing the sort, use the words 'adjacent', 'swap', 'pass' and 'no swaps'.
  • If the question says descending order, swap when the first item is smaller.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 3
    State what a bubble sort doesIt sorts a list by comparing adjacent items and swapping them if they are out of order.
  2. Grade 4
    Show the list after one passCompare each adjacent pair from left to right, swapping where needed.
  3. Grade 5
    Describe the steps of a bubble sortCompare pairs, swap, finish the pass, then repeat passes until a pass makes no swaps.
  4. Grade 6
    Explain how the sort knows it has finishedA pass with no swaps means every pair is in order, so the list is sorted.
  5. Grade 7
    Complete bubble sort code, including the swapUse a temporary variable to swap two items, and a flag to record whether a swap happened.
  6. Grade 8
    Evaluate bubble sort against other sortsSimple and memory-efficient, but slow for large lists compared with merge sort.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

A bubble sort compares two neighbouring items and finds that they are already in the correct order. State what the bubble sort does next.
It leaves them where they are and moves on to the next pair.
Complete the sentences about a bubble sort. Use words from the box. Each word can be used once.
Words: swap, neighbouring, random, delete, sorted
A bubble sort compares ______ items in a list. If two items are in the wrong order, the bubble sort will ______ them.
neighbouring; swap
What is the name for one complete run through the list, comparing neighbouring items, in a bubble sort?
A pass
State one advantage of using a bubble sort.
It is simple to understand and program.

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy3 marks
(a) Which statement describes how a bubble sort works?
Tick (✓) one box.
[1]
  • It splits the list in half repeatedly and then merges the parts back together in order
  • It compares pairs of adjacent items and swaps them if they are in the wrong order
  • It takes each item in turn and inserts it into the correct place in a sorted part of the list
  • It checks each item in turn until the required item is found
(b) State how a bubble sort knows that the list is sorted.
[1]
(c) Give one advantage of a bubble sort.
[1]
Show the answer and mark scheme
(a) Answer: It compares pairs of adjacent items and swaps them if they are in the wrong order
(b)
  • a pass is completed in which no swaps are made
(c)
  • simple to understand / code
  • sorts the list in place / needs very little extra memory
  • quick to check a list that is already sorted (only one pass is needed)
Question 2Medium5 marks
A music app stores a list of 50 000 song titles.
(a) Explain why a bubble sort would not be a good choice for sorting the titles into alphabetical order.
[2]
(b) Name a sorting algorithm that would be more efficient for this list.
[1]
(c) The titles are already in alphabetical order when a bubble sort is run. State the number of passes the bubble sort makes and explain why.
[2]
Show the answer and mark scheme
(a)
  • it only compares / swaps adjacent items, so a long list needs many passes / a very large number of comparisons
  • so it would take a long time / is inefficient for large lists
(b) Answer: Merge sort
  • merge sort
(c)
  • one pass
  • no swaps are made in the first pass, so the algorithm stops
Question 3Hard8 marks
A programmer has written this procedure to sort an array into ascending order using a bubble sort. It contains errors.
01 procedure sortList(list)
02    for pass = 1 to list.length - 1
03       for i = 0 to list.length - 1
04          if list[i] > list[i + 1] then
05             list[i] = list[i + 1]
06             list[i + 1] = list[i]
07          endif
08       next i
09    next pass
10 endprocedure
(a) The swap on lines 05 and 06 does not work. Explain why, using the two items [8, 3] as an example.
[2]
(b) Rewrite lines 05 and 06 so that the swap works correctly.
[2]
(c) Identify the other error in the procedure and explain how to correct it.
[2]
(d) The procedure always makes list.length − 1 passes. Explain how it could be made more efficient.
[2]
Show the answer and mark scheme
(a)
  • line 05 sets list[0] to 3 so the 8 is overwritten / lost
  • line 06 then copies 3 back into list[1], so the list becomes [3, 3]
(b) Answer:
temp = list[i]
list[i] = list[i + 1]
list[i + 1] = temp
  • stores one of the values in a temporary variable before it is overwritten
  • completes the swap correctly using the temporary variable
(c)
  • line 03: i goes up to list.length − 1, so list[i + 1] is past the end of the array / index out of range
  • change line 03 to: for i = 0 to list.length - 2
(d)
  • use a flag to record whether any swap is made during a pass
  • stop sorting when a pass makes no swaps (e.g. replace the outer for loop with a while / do … until loop)
  • make each pass one comparison shorter, because the largest remaining item is already in its final place

More in Searching and sorting algorithms

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.