Bubble sort
Сортировка пузырьком
This sorting algorithm makes repeated passes through a list, swapping neighboring items when they are out of order. When sorting from smallest to largest, each left-to-right pass moves the largest remaining item to the end of the unsorted portion.
How bubble sort works
Start at the left end of an array. Compare the first two items and swap them if the left one is larger. Then move one position to the right and compare the next pair using the array as it looks after the swap. A large item can move several positions in one pass because it keeps getting compared with its next neighbor. By the end of the pass, the largest item in the part you scanned has reached its final position.
On the next pass, stop one position earlier: there is no need to compare the item you just placed. Keep shrinking the part you scan. If a full pass makes no swaps, you can stop early: every neighboring pair in the remaining portion is already in order.
Worked example
Take [3, 1, 4, 2]. On the first pass, swap 3 and 1, leave 3 and 4 alone, then swap 4 and 2. The result is [1, 3, 2, 4], with 4 in its final position. The next pass scans only the first three items and changes them to [1, 2, 3]. The final pass makes no swaps.
def bubble_sort(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]
swapped = True
if not swapped:
break
This function sorts the original list in place rather than building a new one. For an empty list or a single item, the outer loop never runs.
Time, memory, and stability
Bubble sort takes Θ(n²) time on average and in the worst case. For a reverse-sorted list of distinct values, the passes make n − 1, then n − 2, and eventually 1 comparison. With the early-exit check, an already sorted list takes just one pass, so the best case is Θ(n). Without that check, the algorithm still makes all scheduled comparisons even if it never swaps anything.
The implementation uses O(1) extra space: it keeps a few variables and swaps items in the input list. It is also stable, meaning records with equal sort keys keep their original order. For instance, if two tickets have the same priority, sorting by priority alone won't reverse them. Using >= instead of > would allow equal-key neighbors to swap and break that guarantee.
When it comes up in practice and interviews
Bubble sort is mainly a teaching tool. It is easy to trace by hand, and it shows why a simple routine can become expensive as the input grows. In application code, prefer the language's built-in sort unless you have a specific reason to implement your own.
In a coding interview, explain what stays true after every pass: the items on the right are already in their final positions. Then check the two common implementation mistakes. The inner loop must stop before i + 1 goes past the current boundary, and the no-swap check belongs after the entire pass, not after a single comparison. For example, in [1, 3, 2], the first pair is in order, but the second still needs a swap.