Bubble Sort
Sorts by comparing adjacent elements; the larger value is pushed one slot at a time toward the end, which is where the name comes from. Simple to implement, but unsuitable for large data.
01Bubble Sort
Explore How It WorksStarting bubble sort. Compare each neighbouring pair and push the larger one toward the back, over and over.
Compare the first element 45 with the second element 12.
45 > 12, so swap them. The larger value bubbles toward the end.
Compare 45 and 89. 89 is larger, so keep them as is.
Compare 89 and 34, then swap.
Compare 89 and 67, then swap.
Compare 89 and 23, then swap.
Compare 89 and 56, then swap.
Finally, compare 89 and 10, then swap.
First pass complete! The largest value 89 has settled into the last position.
Complete the remaining passes (2–7). Each pass bubbles the largest value toward the end.
Bubble sort complete! [10, 12, 23, 34, 45, 56, 67, 89] — every element is sorted in ascending order.
02 Understand It Simply
For EveryoneCompares neighboring values and swaps them when they are out of order. Each pass settles the largest remaining value at the end.
Repeatedly compares neighbors and swaps them when they're out of order.
Simple but slow (O(n²)), so it's a poor fit for large data.
- –Learning sorting concepts
- –small nearly-sorted data
03 Python Implementation
A clean, readable reference implementation of the core logic of Bubble Sort.
04 Frequently Asked Questions
FAQWhat is Bubble Sort?+
Sorts by comparing adjacent elements; the larger value is pushed one slot at a time toward the end, which is where the name comes from. Simple to implement, but unsuitable for large data.
What is the time complexity of Bubble Sort?+
The time complexity of Bubble Sort is O(n²). Follow the step-by-step visualization to see exactly why.
Where is Bubble Sort used?+
Learning sorting concepts, small nearly-sorted data.
What's a simple analogy for Bubble Sort?+
Compares neighboring values and swaps them when they are out of order. Each pass settles the largest remaining value at the end.
