Skip to content

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:

Interactive diagram — enable JavaScript to view.

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.

Sorting Algorithms in Python

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.

intermediate algorithms python

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated Aug. 5, 2026