Time Complexity
Best: O(n)
Avg: O(n²)
Worst: O(n²)
Bubble Sort is a simple comparison-based sorting algorithm. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
The algorithm gets its name because smaller elements "bubble" to the top of the list with each pass.
How it works:
1. Compare each pair of adjacent elements
2. Swap them if the left element is greater
3. After each pass, the largest unsorted element settles in its final position
4. Repeat until no swaps are needed
Time Complexity:
Best: O(n) — already sorted
Average: O(n²)
Worst: O(n²)
Space Complexity: O(1) — in-place
Properties:
- Stable sort
- Adaptive
- In-place
Mainly used for educational purposes. For production, prefer Quick Sort or Merge Sort.
Related algorithms
Frequently asked questions
- What is Bubble Sort?
- Bubble Sort is a simple comparison-based sorting algorithm. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
- What is the complexity of Bubble Sort?
- Time (average): O(n²) · Space: O(1)
- Who is this Bubble Sort visualizer for?
- The Bubble Sort visualization targets beginner-level learners in the Sorting category. Useful for students, interview prep, and hands-on review.
- What algorithms are related to Bubble Sort?
- In the same category (Sorting) you can explore: Selection Sort, Insertion Sort, Quick Sort. Each has an interactive visualization.