Bubble Sort Visualization & Animation
Repeatedly swaps adjacent elements until the array is sorted — simple, O(n²), good for teaching.
## What is it?
Bubble Sort is the simplest sorting algorithm. It repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. Larger elements "bubble up" to the end with each pass.
## How it works
- Compare element at index `i` with element at `i+1`
- If `arr[i] > arr[i+1]`, swap them
- After each full pass, the largest unsorted element is placed at its correct position
- Repeat for `n-1` passes total
- **Optimization:** if no swap occurred in a pass, the array is already sorted — stop early
## When to use
- Teaching / understanding sorting concepts
- Nearly sorted arrays (with the early-exit optimization, it can approach O(n))
- When simplicity matters more than performance
## Key Points
- Stable sort — equal elements keep their original order
- In-place — no extra memory needed
- Worst case occurs when the array is reverse-sorted
Category: algorithms
Difficulty: beginner
Time Complexity: O(n²)
Space Complexity: O(1)
View Bubble Sort Visualization