Bubble Sort Simulation

Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order, pushing the largest unsorted value to the end of each pass.

Interactive simulation

Pass
1
Comparing
-
Swaps
0
Sorted tail
0
Speed
1x
Ready: press Compare Next or Resume to start

Legend

  • Unsorted element
  • Comparing pair
  • Swapping values
  • Sorted in final position

Event log

    How to use this simulation

    1. Press Compare Next once to step through one adjacent comparison/swap at a time, or press Resume to animate continuously.
    2. Watch how the highlighted pair is compared and swapped when left > right, locking each largest item at the right end.
    3. Use Step to jump to the next event, Slower (down to 0.25x) and Faster (up to 4x) to change speed, and Reset to generate a new array.

    1. Adjacent comparisons

    In pass k, Bubble Sort steps from index 0 up to n - k - 1, comparing arr[j] with arr[j + 1].

    2. Swapping out-of-order pairs

    Whenever arr[j] > arr[j + 1], the two elements swap places so heavier values drift rightward toward their sorted spots.

    3. Time & space complexity

    Worst-case and average time complexity are O(n²), with O(1) auxiliary space. With an early-exit flag when no swaps occur in a pass, best-case time on an already-sorted array is O(n).

    Related topics