Bubble Sort Visualizer

Watch Bubble Sort compare and swap adjacent values with step controls, playback speed, operation metrics, and an interactive array.

Current status: Comparing the highlighted values to decide the next move.

Comma-separated numbers, up to 20 values.

Balanced

Live operation

compare

Step 1 / 22
5
[0]
3
[1]
8
[2]
4
[3]
2
[4]
CompareSwapSorted

Comparisons

1

Moves / writes

0

Confirmed sorted

0 / 5

Algorithm notes

What to watch for

Bubble Sort repeatedly scans adjacent pairs. A swap pushes the larger value rightward; after every pass, the far-right unsorted value is permanently placed.

Concept guide

Review the mental model, tradeoffs, and practical use cases after you experiment.

Bubble Sort Complete Info Card

Comparison SortIn-Place

Bubble Sort repeatedly compares adjacent elements and swaps them if they're in the wrong order, causing larger values to "bubble" to the end with each pass.

Algorithm Characteristics

Time Complexity (Best)

When array is already sorted

O(n)

Time Complexity (Average)

Typical case with random data

O(n²)

Time Complexity (Worst)

When array is reverse sorted

O(n²)

Space Complexity

In-place sorting algorithm

O(1)

Stable

Maintains relative order of equal elements

Yes

Adaptive

Can optimize for nearly-sorted inputs

Yes

Sorting Process Steps

1

Start with first two elements

2

Compare and swap if out of order

3

Move to next pair, repeat to end

4

Largest element "bubbles" to end

5

Repeat process for n-1 passes

6

Optimize with early exit if no swaps

Optimization Techniques

Early Termination

Stop if no swaps occur in a pass

if (noSwaps) break;

Reduced Passes

Ignore already sorted elements

for (let j = 0; j < arr.length-i-1; j++)

When to Use

  • Educational purposes (simple to understand)
  • Nearly sorted small datasets
  • Memory-constrained environments

When to Avoid

  • Large datasets (use QuickSort/MergeSort)
  • Performance-critical applications
  • When stability isn't required
Did You Know? Bubble Sort gets its name because elements "bubble" to their correct position like bubbles rising in water.
Teaching toolTiny datasetsDebugging aid