Playground

Bubble Sort

Watch adjacent elements swap in real time as the largest values bubble to the end of the array.

Bubble Sort

Repeatedly swap adjacent elements that are out of order. After each pass the largest unsorted element bubbles to its final position.

Time O(n²)  |  Space O(1)  |  Stable  |  In-place
Data
Size15
Speed800ms
380271432339482510655734876912106111501218139014
default
comparing
swapping
sorted

BUBBLE SORT

for i = 0 to n-2:
for j = 0 to n-i-2:
if arr[j] > arr[j+1]:
swap(arr[j], arr[j+1])
// largest bubbled to end
// array is sorted

Console Output

n = 15
>Press SORT to begin…