Lesson 106

Bubble Sort

Compare • Swap • Bubble Up

1:00

How bubble sort works — the compare-and-swap rule, why large values "bubble" to the end each pass, the early-exit optimization, and O(n²) / O(1) space complexity.

By the end, you can

  • Trace bubble sort step by step on a small array, showing the state after each pass.
  • Explain why each pass locks the largest unsorted element at the end of the unsorted region.
  • Identify how many comparisons and swaps a specific pass performs.
  • Explain the early-exit optimization and state its effect on best-case complexity.
  • State the worst-case, best-case, and space complexity of bubble sort and justify each.
  • Explain what "stable sort" means and why bubble sort qualifies.
  • Identify when bubble sort is appropriate and why insertion sort is preferred in production.
Up next in Searching & Sorting
Questions or feedback?