Algorithm Visualizer

Bubble Sort Algorithm

Visualize how bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

Bubble Sort: Step 0

Press start to begin the visualization.

Visualization
Color Legend: Blue = Unsorted, Yellow = Comparing, Red = Swapping, Green = Sorted
Current array: []
Animation Speed
Step: 0 / 0
How Bubble Sort Works

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

  1. Start at the beginning of the array.
  2. Compare the current element with the next element.
  3. If the current element is greater than the next element, swap them.
  4. Move to the next element and repeat steps 2-3 until the end of the array.
  5. After one complete pass, the largest element will be at the end of the array.
  6. Repeat steps 1-5, but each time reduce the array length by 1 (as the end is already sorted).
  7. Continue until no more swaps are needed.

Complexity

  • Time Complexity: O(n²) in worst and average cases
  • Space Complexity: O(1) - only requires a constant amount of additional memory space
  • Best Case: O(n) when the array is already sorted