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
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
- Explore the Sorting Algorithms visualizer: Bubble, selection, insertion, merge and quick sort animated.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Bubble Sort?
Where do people use Bubble Sort?
What is the most common mistake with Bubble Sort?
What should I learn before this topic?
How can I practice it?
Is Bubble Sort hard to learn?
How long does it take to learn Bubble Sort?
Which topics are related to Bubble Sort?
What should I study after Bubble Sort?
Related topics
- Binary Search (Algorithms)
- Selection Sort (Algorithms)
- Linear Search (Algorithms)
- Insertion Sort (Algorithms)