2024-05-02
Written by: Juri Breslauer
Algorithm Notes
Bubble sort compares two neighboring values. If the left value is bigger, they swap places. After each pass, the largest unsorted value has moved to the right side.
Interactive demo
The animation uses a small set of bars as an example. On every step, bubble sort looks at two neighbors:
Bubble sort is easy to understand, but it is not efficient for large inputs.
In the average and worst case it needs O(n²) comparisons. That is fine for a
small visual demo. For thousands of items, a better sorting algorithm is usually
the right choice.
Here are examples of the same bubble sort algorithm in different programming languages.