LearnViz CompSci
Home / Learn / Algorithms / Bubble Sort

Beginner Topic 8 of 51 Algorithms

Bubble Sort

Repeatedly swapping adjacent out-of-order pairs.

How it works

Bubble sort repeatedly steps through the list, compares adjacent elements and swaps them if they are out of order, so the largest values bubble to the end after each pass. It runs in O(n squared) time in the average and worst cases, uses O(1) extra space and is stable. With an early-exit flag, an already sorted list needs only one O(n) pass.

Where it is used

It is mostly used for teaching sorting concepts, visualizing algorithms and sorting tiny or nearly sorted lists where simplicity matters. Real systems prefer faster algorithms.

Common pitfall

Its quadratic cost makes it unusable for large inputs, and it does many more swaps than selection sort. Forgetting to shrink the range after each pass wastes work, and omitting the early-exit check loses the best-case benefit. Use insertion sort or the library sort instead.

Where it fits in the course

Bubble Sort is topic 8 of 51 in Algorithms, rated Beginner. It follows Binary Search. The next topic is Selection Sort.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Bubble Sort?
Repeatedly swapping adjacent out-of-order pairs. It belongs to Algorithms and is a beginner-level topic.
Where do people use Bubble Sort?
It is mostly used for teaching sorting concepts, visualizing algorithms and sorting tiny or nearly sorted lists where simplicity matters.
What is the most common mistake with Bubble Sort?
Its quadratic cost makes it unusable for large inputs, and it does many more swaps than selection sort.
What should I learn before this topic?
Start with Binary Search, the topic just before it in Algorithms.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Algorithms quiz. You can also explore Sorting Algorithms in the Visual Lab.
Is Bubble Sort hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn Bubble Sort?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Bubble Sort?
Binary Search, Selection Sort, Linear Search are closely connected. The full list is in the related topics section of this page.
What should I study after Bubble Sort?
Continue with Selection Sort, the next topic in Algorithms.

Related topics