Bubble Sort

Computer Science: OCR A Level, topic 8.1.5

Bubble Sort

Bubble sort is a naive sorting algorithm.

The concept

The concept

  • Imagine that you have a set of cards face up on the desk.
  • If the first two cards are in the wrong order, you swap them.
  • Then do the same for the second and third cards, and continue in this pattern until the end of the pack. This is known as a pass.
  • The highest value will 'bubble' up to the top of the pack each pass.
  • By repeating this enough times, the pack will get sorted.
In English

In English

  • Compare the first two items of the dataset:
  • If they are in the wrong order, swap them.
  • Continue for the rest of the cards in the deck.
  • Repeat the whole process, until a pass with no swaps happens.
In pseudocode

In pseudocode

Pros and cons of bubble sort

Pros and cons of bubble sort

  • Pros:
  • Easy to implement.
  • Does not use much memory.
  • Cons:
  • Poor for efficiency.

Test yourself on Bubble Sort

  • What is the main disadvantage of bubble sort?

  • Bubble Sort in English

Practise this topic for free