Back to Benchmark

Bubble Sort - O(n²) Complexity

Understanding why bubble sort has quadratic time complexity

What is Bubble Sort?

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates the list is sorted.

Key Characteristics:

  • Simple Algorithm: Easy to understand and implement
  • In-Place: Sorts array in place, O(1) extra space
  • Stable: Preserves relative order of equal elements
  • O(n²) Time: Quadratic time complexity - slow for large arrays

Think of it like bubbles rising to the surface - larger elements "bubble up" to the end of the array with each pass!

How Does Bubble Sort Work?

1. Compare Adjacent Elements

Start from the beginning and compare each pair of adjacent elements. If they are in the wrong order (first > second), swap them.

2. Bubble Up Largest Element

After one complete pass through the array, the largest element will have "bubbled up" to the end. This element is now in its correct sorted position.

3. Repeat for Remaining Elements

Repeat the process for the remaining unsorted portion (excluding the last element which is already sorted). Continue until no swaps occur in a pass.

4. Quadratic Time Complexity

For an array of n elements:

Pass 1: Compare n-1 pairs
Pass 2: Compare n-2 pairs
Pass 3: Compare n-3 pairs
...
Pass n-1: Compare 1 pair
Total comparisons: (n-1) + (n-2) + ... + 1 = n(n-1)/2 = O(n²)
Time complexity: O(n²)
Interactive Bubble Sort Visualization
Watch how bubble sort compares and swaps adjacent elements!
0
83
1
15
2
99
3
60
4
39
5
20
6
14
7
21

Code Example:

// Bubble Sort - O(n²)
function bubbleSort(arr) {
  const n = arr.length;

  // Outer loop: number of passes
  for (let i = 0; i < n - 1; i++) {
    let swapped = false;

    // Inner loop: compare adjacent pairs
    for (let j = 0; j < n - i - 1; j++) {
      // Compare and swap if needed
      if (arr[j] > arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
        swapped = true;
      }
    }

    // Early termination if no swaps
    if (!swapped) break;
  }

  return arr;
}
Built-in Functions for Sorting
Most programming languages provide built-in sorting functions that use efficient algorithms (not bubble sort). However, understanding bubble sort helps learn sorting concepts

Why is Bubble Sort O(n²)?

1. Nested Loops

Bubble sort uses two nested loops:
• Outer loop: n-1 passes (one less than array size)
• Inner loop: compares n-i-1 pairs in pass i
• Total comparisons: (n-1) + (n-2) + ... + 1 = n(n-1)/2

2. Quadratic Growth

As array size doubles, comparisons quadruple:

10 elements → ~45 comparisons
20 elements → ~190 comparisons (4× more)
100 elements → ~4,950 comparisons (100× more)
1,000 elements → ~499,500 comparisons (10,000× more!)
This is quadratic growth: O(n²)

3. Comparison with Efficient Sorts

Bubble sort vs efficient algorithms:

Bubble sort: O(n²) - Very slow
Quick/Merge/Heap sort: O(n log n) - Much faster
For 1,000 elements: O(n²) = 1,000,000 ops vs O(n log n) = 10,000 ops
Bubble sort is 100× slower!

⚠️ Why Bubble Sort is Rarely Used

Bubble sort is simple to understand but extremely inefficient. It's mainly used for educational purposes to teach sorting concepts. In practice, always use O(n log n) algorithms like quicksort, merge sort, or heap sort.

Time Complexity Comparison
AlgorithmBestAverageWorstSpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Quick SortO(n log n)O(n log n)O(n²)O(log n)No
Merge SortO(n log n)O(n log n)O(n)Yes
Heap SortO(n log n)O(n log n)O(n log n)O(1)No
Real-World Examples

📚 Educational Purposes

Bubble sort is primarily used in computer science education to teach sorting concepts. Its simplicity makes it easy to understand how sorting algorithms work, even though it's not practical for real applications.

🔍 Understanding Complexity

Learning bubble sort helps understand why O(n²) algorithms are inefficient. Comparing it to O(n log n) algorithms demonstrates the importance of choosing the right algorithm.

Very Small Arrays

For extremely small arrays (3-5 elements), bubble sort's simplicity might make it acceptable, though even then, insertion sort is usually better. However, this is extremely rare in practice.

🎯 Interview Questions

Bubble sort is sometimes asked in coding interviews to test basic understanding of sorting, though candidates are expected to know it's not practical and should suggest better alternatives.

❌ What NOT to Use It For

Never use bubble sort in production code! For any real application, use built-in sort functions (which use O(n log n) algorithms) or implement quicksort, merge sort, or heap sort. Bubble sort is simply too slow.

Key Takeaways
  • ⚠️
    Bubble sort is O(n²) - quadratic time

    Nested loops result in n(n-1)/2 comparisons, making it extremely slow for large arrays

  • 📚
    Educational value only

    Simple to understand and implement, but never use in production code

  • Stable and in-place

    Preserves relative order and uses O(1) space, but these benefits don't outweigh the O(n²) time complexity

  • Always use O(n log n) algorithms

    For any real application, use quicksort, merge sort, heap sort, or built-in sort functions

Practice Exercises
Test your understanding of bubble sort with these exercises. Try to solve them yourself before checking the answers!