bubble sort
Bubble sort is a comparison sorting algorithm that repeatedly steps through a list, compares each pair of adjacent elements, and swaps them whenever they’re out of order. Each pass moves the largest remaining value into its final position, and the passes repeat until one completes with no swaps, which means the list is sorted.
Step through the visualizer below one comparison at a time and watch the sorted tail grow from the right, locking in one more value after every pass:
The name reflects how large values gradually rise toward the end of the list, the way a bubble rises to the surface. Because it only compares neighbors and swaps them in place, bubble sort uses no extra memory and never reorders equal elements, which makes it both stable and in-place.
Its main limitation is speed. Bubble sort runs in O(n²) time in the average and worst cases, so it scales poorly to large inputs. Because the sort stops as soon as a pass makes no swaps, an already-sorted list finishes in a single pass, giving a best case of O(n). Faster methods such as quicksort and merge sort dominate in practice, leaving bubble sort mainly as a teaching example for the mechanics of sorting.
Related Resources
Tutorial
Sorting Algorithms in Python
In this tutorial, you'll learn all about five different sorting algorithms in Python from both a theoretical and a practical standpoint. You'll also learn several related and important concepts, including Big O notation and recursion.
For additional information on related topics, take a look at the following resources:
- Introduction to Sorting Algorithms in Python (Course)
- Lists vs Tuples in Python (Tutorial)
- Look Ma, No for Loops: Array Programming With NumPy (Tutorial)
- Recursion in Python: An Introduction (Tutorial)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
- Recursion in Python (Course)
- Recursion in Python: An Introduction (Quiz)
By Martin Breuss • Updated Aug. 5, 2026