Bubble Sort Animation
–
Waiting for input...
Color: Compare • Swap • Sorted
Visualize bubble sort step by step - watch adjacent comparisons and swaps animate in real time.
Time O(n²) · Space O(1) · Stable
Waiting for input...
Color: Compare • Swap • Sorted
def bubble_sort(arr) -> list[int]:
if arr is None or len(arr) == 0:
raise ValueError("Array is empty")
for step in range(len(arr)):
for i in range(len(arr) - 1 - step):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
return arr
Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This pass-through is repeated until the list is sorted. It is one of the simplest sorting algorithms to understand and implement.
Starting from the first element, compare each pair of adjacent elements and swap them if the left one is greater. After each full pass, the largest unsorted element 'bubbles up' to its correct position. Repeat until no swaps are needed.