Sorting Algorithms· 15 min

Bubble Sort

Compare adjacent pairs and swap — the simplest sort

Setup

Size20
Step 1 / 552

Current step

1 / 552

Action: Starting Bubble Sort. We'll compare adjacent pairs and bubble larger elements to the end.

Why: This is the next operation required by the algorithm.

Result: The visualization now reflects this operation.

Step log

18
9
36
17
25
6
29
11
53
59
51
32
40
52
37
9
12
57
55
53
012345678910111213141516171819
Unsorted
Comparing
Swap needed
Swapping
Moving
Pivot
Sorted
bubble.java
1void bubbleSort(int[] arr) {
2 int n = arr.length;
3 for (int i = 0; i < n - 1; i++) {
4 for (int j = 0; j < n - 1 - i; j++) {
5 if (arr[j] > arr[j + 1]) {
6 int t = arr[j];
7 arr[j] = arr[j + 1];
8 arr[j + 1] = t;
9 }
10 }
11 }
12}
● Java

Bubble Sort

Repeatedly steps through the list, compares adjacent pairs and swaps them if they are in the wrong order. Simple but inefficient on large lists.

Time complexity
O(n²)
Space
O(1)
Stable
Yes
Comparisons
175
n = 201 / 552 steps