Bubble Sort
- In Turkish
- kabarcık sıralaması
- Pronunciation
- BUB-ul SORT
In short
Bubble sort is a simple sorting algorithm that keeps swapping out-of-order neighbors, so each pass carries the largest remaining value to the end.
What is bubble sort?
Each pass compares every pair of neighbors and swaps them if the left one is bigger. After the first pass, the largest element has moved all the way to the end; after the second, the second largest sits just before it, and so on. The sorted part grows from the right until a pass makes no swaps, which means the list is in order.
Bubble sort takes O(n²) time in the average and worst cases, because each of up to n passes may compare up to n pairs. With the common optimization of stopping when a pass makes no swaps, an already sorted list takes just one pass, O(n). It sorts in place with O(1) extra memory and is stable, keeping equal elements in their original order.
Its value is educational. It is easy to understand, visualize and implement, which makes it a common first sorting algorithm and a good way to learn about loops, swaps, complexity and stability. Watching it run shows clearly why some algorithms scale far better than others.
A common misconception is that bubble sort is acceptable for real data. It is far slower than the alternatives on anything but tiny or nearly sorted inputs; even insertion sort, which is also O(n²), usually beats it. Production code should use the language's built-in sort, which uses efficient algorithms such as Timsort or introsort.
Key takeaways
- Bubble sort repeatedly swaps neighboring elements that are out of order.
- Each pass moves the largest remaining element to the end.
- It is O(n²) on average, O(n) on sorted input with early exit.
- It is in-place and stable, and mainly used for teaching.
- Real code should use the built-in sort instead.
Example
def bubble_sort(items):
items = list(items)
for end in range(len(items) - 1, 0, -1):
swapped = False
for i in range(end):
if items[i] > items[i + 1]:
items[i], items[i + 1] = items[i + 1], items[i] # swap neighbors
swapped = True
if not swapped: # no swaps: already sorted, stop early
break
return items
print(bubble_sort([5, 1, 4, 2, 8])) # [1, 2, 4, 5, 8]Readers ask
Why is bubble sort slow?
Because it moves elements only one position at a time and may need about n passes over n elements, giving O(n²) comparisons. Doubling the input roughly quadruples the work.
Is bubble sort stable?
Yes. It only swaps neighbors when the left one is strictly greater, so equal elements never pass each other and keep their original order.
What is the difference between bubble sort and insertion sort?
Both are O(n²) in the worst case. Bubble sort swaps neighbors across the whole list on each pass, while insertion sort builds a sorted prefix and inserts each new element into place, which usually does far fewer operations.
See also
- Sorting AlgorithmData Structures, p. 30A sorting algorithm is a step-by-step method for arranging items in a defined order, such as numbers from smallest to largest or names alphabetically.
- Insertion SortData Structures, p. 20Insertion sort builds a sorted list one element at a time, putting each new one in its place among those already sorted, like sorting cards in your hand.
- Merge SortData Structures, p. 24Merge sort is a divide and conquer sorting algorithm that splits a list in half, sorts each half recursively, and merges the sorted halves in O(n log n) time.
- QuicksortData Structures, p. 27Quicksort is a divide and conquer sorting algorithm that partitions items around a chosen pivot, then sorts the smaller and larger groups the same way.
- Big O NotationProgramming Fundamentals, p. 6Big O notation describes how an algorithm's running time or memory use grows as its input gets larger, focusing on the growth rate rather than exact speed.
- ArrayProgramming Fundamentals, p. 3An array is an ordered collection of values stored under one name, where each item is accessed by its numeric position, called an index, usually starting at 0.
Spotted a mistake or something missing on this page?Suggest an edit