Bubble Sort Visualizer

What is Bubble Sort

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. It gets its name because smaller elements "bubble" to the top of the list.

How Does It Work

Imagine you have an unsorted list of numbers:[5, 1, 4, 2, 8]
  1. First Pass:
    • (5, 1) → Swap → [1, 5, 4, 2, 8]
    • (5, 4) → Swap → [1, 4, 5, 2, 8]
    • (5, 2) → Swap → [1, 4, 2, 5, 8]
    • (5, 8) → No swap
  2. Second Pass:
    • (1, 4) → No swap
    • (4, 2) → Swap → [1, 2, 4, 5, 8]
    • (4, 5) → No swap
  3. Third Pass: No swaps needed → List is sorted

The algorithm stops when a complete pass is made without any swaps.

Algorithm Steps

  1. Start with an unsorted array
  2. Set a flag to track if any swaps occur
  3. For each pair of adjacent elements:
    • Compare the two elements
    • If they are in the wrong order, swap them
    • Set the swap flag to true
  4. Repeat the process until a complete pass is made without any swaps
  5. The array is now sorted

Time Complexity

  1. Best Case: Array is already sorted → O(n) (only one pass needed).
  2. Average Case: Randomly ordered array → O(n²).
  3. Worst Case: Array is sorted in reverse order → O(n²).

Space Complexity

Bubble Sort is an in-place sorting algorithm, meaning it requires only O(1) additional space (for temporary storage during swaps).

Bubble Sort is simple to understand and implement but inefficient for large datasets. It's mainly used for educational purposes to introduce sorting algorithms. In practice, more efficient algorithms like QuickSort or MergeSort are preferred.

Watch Bubble Sort in action as it repeatedly swaps adjacent elements to sort the array step by step.

Speed:1x
Comparisons:
0
Swaps:
0

Array Visualization

Generate or enter an array to begin