Bubble Sort
Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
BeginnerTime: O(n²)Space: O(1)Stable
Current Step
Press Play to start the visualization
## How Bubble Sort Works
Bubble Sort is a simple comparison-based sorting algorithm. It works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order.
### Algorithm Steps:
1. Start from the first element
2. Compare the current element with the next element
3. If current > next, swap them
4. Move to the next pair and repeat
5. After one pass, the largest element "bubbles up" to the end
6. Repeat for remaining unsorted elements
### Key Characteristics:
- **In-place**: Only requires O(1) extra space
- **Stable**: Equal elements maintain their relative order
- **Adaptive**: Can detect if array is already sorted (best case O(n))
Time & Space Complexity
| Case | Time Complexity |
|---|---|
| Best | O(n) |
| Average | O(n²) |
| Worst | O(n²) |
| Space | O(1) |
Practice Problems
10 problems81 total points
1
Basic Bubble Sort
Beginner5 pts
2
Count Swaps
Beginner5 pts
3
Sort Descending
Beginner5 pts
4
Optimized Bubble Sort
Intermediate8 pts
5
Sort by Absolute Value
Intermediate8 pts
6
Cocktail Shaker Sort
Advanced10 pts
7
Sort Linked List
Advanced10 pts
8
K Sorted Array
Advanced10 pts
9
Sort with Minimum Swaps
Advanced10 pts
10
Sort Matrix Rows
Advanced10 pts