The worst-case time complexity of bubble sort is O(n²). This occurs when the input array is reverse-sorted, requiring the maximum number of comparisons and swaps.

Why O(n²)?

Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. In the worst case—like sorting —every pass bubbles the largest unsorted element to its position, but the algorithm makes n-1 passes, each with up to n-1 comparisons. This nested loop structure results in roughly n(n-1)/2 operations, simplifying to quadratic time, O(n²).

Optimized versions (e.g., stopping early if no swaps occur) don't change the worst case, though they improve best-case to O(n).

Complexity Overview

Case| Time Complexity| Scenario Description 357
---|---|---
Worst| O(n²)| Reverse-sorted array; all swaps needed.
Average| O(n²)| Random order; ~n²/2 comparisons.
Best| O(n)| Already sorted (optimized version).
Space| O(1)| In-place sorting, no extra space beyond variables.

Real-World Example

Imagine sorting:

  • Pass 1: Compares/swaps to get.
  • Continues until sorted after multiple full traversals.
    For n=100,000, O(n²) means up to 10 billion operations—impractical for large datasets, where quicksort (O(n log n) average) shines.

When It Matters

Bubble sort teaches fundamentals but scales poorly. Forums like Stack Overflow note its O(n²) is theoretical yet real: doubling input size quadruples time. As of 2025 analyses, it's still a baseline for comparison—no major "trending" shifts, but DSA courses emphasize it for interviews.

TL;DR: Bubble sort's worst-case is O(n²) due to full nested loops on reverse input.

Information gathered from public forums or data available on the internet and portrayed here.