Level Up Your Coding Skills & Crack Interviews — Save up to 50% or more on Educative.io Today! Claim Discount

Arrow
Table of contents

Bubble Sort Algorithm

Bubble Sort is one of the simplest sorting algorithms. It repeatedly compares neighboring elements and swaps them when they are in the wrong order.

What is Bubble Sort?

Bubble Sort is a comparison-based sorting algorithm that sorts an array by repeatedly comparing adjacent elements. If two neighboring elements are in the wrong order, the algorithm swaps them.

After each full pass through the array, the largest unsorted element moves to its correct position at the end. This is why the algorithm is called Bubble Sort: larger elements “bubble up” toward the end of the array.

Interview perspective: Bubble Sort is not commonly used as the optimal solution in coding interviews, but it is useful for understanding comparison-based sorting, swaps, nested loops, and basic time complexity analysis.

Core Intuition

Imagine the array as a line of numbers. Bubble Sort only looks at two neighbors at a time.

If the left number is greater than the right number, they are out of order, so Bubble Sort swaps them. By doing this repeatedly, the largest number gradually moves to the far right.

What moves?

Larger elements move rightward one swap at a time.

What becomes fixed?

After every pass, the largest remaining unsorted element is fixed at the end.

Bubble Sort Algorithm Steps

  1. Start from the beginning of the array.
    Compare the first pair of adjacent elements.
  2. Swap if needed.
    If the left element is greater than the right element, swap them.
  3. Move one position forward.
    Compare the next adjacent pair and repeat the same logic.
  4. Complete one full pass.
    At the end of the first pass, the largest element is at its correct position.
  5. Repeat for the remaining unsorted elements.
    Ignore the sorted suffix at the end and continue until the array is sorted.

Interactive Bubble Sort Visualization

Use the controls below to see how Bubble Sort compares adjacent elements, swaps them, and gradually builds the sorted part of the array from right to left.

Dry Run Example

Suppose we want to sort:

[5, 1, 4, 2, 8]

Pass 1

  • Compare 5 and 1 → swap → [1, 5, 4, 2, 8]
  • Compare 5 and 4 → swap → [1, 4, 5, 2, 8]
  • Compare 5 and 2 → swap → [1, 4, 2, 5, 8]
  • Compare 5 and 8 → no swap → [1, 4, 2, 5, 8]

At the end of Pass 1, the largest element, 8, is already in the correct position.

Pass 2

  • Compare 1 and 4 → no swap
  • Compare 4 and 2 → swap → [1, 2, 4, 5, 8]
  • Compare 4 and 5 → no swap

The array is now sorted.

Bubble Sort Code in Python

This implementation includes a useful optimization: if a complete pass finishes without any swap, the array is already sorted, and the algorithm stops early.

Time and Space Complexity

CaseTime ComplexityWhy?
Best CaseO(n)The array is already sorted, and the optimized version stops after one pass.
Average CaseO(n²)The algorithm uses nested loops and performs many adjacent comparisons.
Worst CaseO(n²)The array is sorted in reverse order, so many swaps are required.
Space ComplexityO(1)Bubble Sort sorts the array in place and uses only a few extra variables.

When Should You Use Bubble Sort?

Good for

  • Learning basic sorting
  • Understanding swaps
  • Small arrays
  • Nearly sorted arrays with early stopping

Avoid for

  • Large arrays
  • Performance-critical systems
  • Most real interview solutions
  • Cases where O(n log n) sorting is expected

Important: Bubble Sort is easy to understand, but it is usually not the algorithm you should choose for an optimized interview solution.

Interview Notes and Common Pitfalls

This section highlights the key concepts interviewers expect you to understand, along with common mistakes that can cost you points during coding interviews.

What interviewers may expect you to know

  • Bubble Sort repeatedly compares adjacent elements.
  • After each pass, the largest unsorted element reaches its final position.
  • Its average and worst-case time complexity is O(n²).
  • It can be optimized with a swapped flag.
  • It is stable because equal elements are not swapped out of their original order.
  • It is in place because it does not require another array.

Common mistakes

  • Looping too far and accessing nums[j + 1] out of bounds.
  • Forgetting that the last i elements are already sorted after i passes.
  • Claiming the best case is O(n²) even when using the early-stop optimization.
  • Using Bubble Sort when a faster sorting algorithm is required.

Related coding interview problems

  • Sort an Array
  • Sort Colors
  • Move Zeroes
  • Merge Sorted Array
  • Squares of a Sorted Array

Quick Quiz

Question 1: What happens after one complete pass of Bubble Sort?

The largest unsorted element moves to its correct position at the end of the unsorted section.

Question 2: Is Bubble Sort stable?

Yes. Bubble Sort is stable because it only swaps when the left element is greater than the right element, not when they are equal.

Key Takeaways

  • Bubble Sort compares adjacent elements and swaps them if they are out of order.
  • Each pass places the largest remaining element in its final position.
  • It is simple, stable, and in-place.
  • Its average and worst-case time complexity is O(n²).
  • It is best used for learning, not for optimized interview solutions.

Share with others:

Unlock up to 68% off lifetime access to Coding Interview prep with Educative

Getting ready for coding interviews or sharpening your problem-solving skills? Unlock a lifetime discount with comprehensive resources designed to help you master technical interviews.

Data structures and algorithms

Pattern-based problem solving

Mock interview practice

Real-world coding challenges

Coding Interview Logo