Bubble Sort
Repeatedly swap neighbouring items that are out of order.
About this topic
Bubble sort is a simple sorting algorithm that repeatedly steps through a list, compares neighbouring items and swaps them if they are in the wrong order. After each pass the largest unsorted item bubbles up to its correct position at the end, like a bubble rising in water. The process stops when a full pass makes no swaps.
For example, sorting [5, 2, 4]: compare 5 and 2 and swap to get [2, 5, 4]; compare 5 and 4 and swap to get [2, 4, 5]. A second pass finds no swaps, so the list is sorted. For n items, up to n passes of n comparisons are needed, so the time is O(n squared) in the worst case, though already sorted data takes only one pass if you stop early.
Bubble sort is easy to understand and code, so it is great for learning, but it is too slow for large lists. Real programs use faster methods such as merge sort or quicksort. A common mistake is going beyond the end of the list when comparing neighbours, or not shortening later passes. Practise tracing it by hand with a short list.
Key ideas
- Simple but slow O(n²)
- Large values bubble up
- Stops when no swaps
Quick check
Use cases: where Bubble Sort helps
- Homework: review the key idea behind Bubble Sort before you start a problem set.
- Exam revision: use the quick check to test whether you can recall Bubble Sort without notes.
- Visual learning: play with the interactive lab to see how Bubble Sort changes when you adjust its inputs.
- Teaching a friend: explain Bubble Sort in your own words, then compare your version with the key ideas.
Computing ideas run phones, search engines, games and the wider internet.
Frequently asked questions
What is Bubble Sort?
Repeatedly swap neighbouring items that are out of order.
What will I learn in Bubble Sort?
Simple but slow O(n²). Large values bubble up. Stops when no swaps.
Is Bubble Sort part of Computer Science?
Yes. It is one of 80 Computer Science topics on LearnViz HighSchoolEdu, with 3 key ideas, a live animation and a quick check.
Does the Bubble Sort lab work on a phone or tablet?
Yes. The animation resizes to fit your screen and supports touch, so you can drag, tap and slide with your finger.
How should I study Bubble Sort?
Read the summary and key ideas first, play with the interactive lab to see the idea in motion, then answer the quick check. If you get a question wrong, go back to the key idea it tests.
Do I need an account, and is my progress saved?
No account is needed. Marking a topic as learned is saved in your own browser, so it stays on that device and is not sent to a server.