SortingSorting
Bubble Sort
Repeatedly swap adjacent out-of-order pairs so the largest remaining element bubbles to the end each pass.
29
0
10
1
14
2
37
3
13
4
5
5
42
6
21
7
1/54Start with 8 unsorted elements. Each pass bubbles the largest remaining element to the end.
ComparingSwappedIn final position
PseudocodeLearn Bubble Sort →
1for i in 0 .. n-1:2 swapped = false3 for j in 0 .. n-i-2:4 if a[j] > a[j+1]:5 swap(a[j], a[j+1])6 swapped = true7 if not swapped: breakComplexity
best O(n)
avg O(n²)
worst O(n²)
space O(1)
Speed