DSA
    beginner

    Bubble Sort

    Bubble Sort Algorithm

    O(n²)
    Pass 0

    Bubble Sort: Pass 0

    Starting Bubble Sort with 7 elements

    64
    [0]
    34
    [1]
    25
    [2]
    12
    [3]
    22
    [4]
    11
    [5]
    90
    [6]
    Unsorted
    Comparing
    Sorted
    Step
    1
    Pass
    0
    Comparisons
    0
    Swaps
    0

    Current array: [64, 34, 25, 12, 22, 11, 90] (length: 7)

    One pass of bubble sort

    Sequence

    Order what happens during a single pass.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    What is the worst-case time complexity of Bubble Sort?

    The short version

    Imagine air bubbles in water. Heavier ones sink, lighter ones float up. In Bubble Sort, the largest element 'bubbles up' to the end of the list in each pass.

    In more depth

    How It Works

    Bubble Sort is the "hello world" of sorting algorithms. It works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

    Key Concepts:

    1.Bubbling Up: In each pass, the largest unsorted element "bubbles up" to its correct position at the end of the array.
    2.Optimized Pass: If a pass completes with no swaps, the array is already sorted, and we can terminate early.

    Why learn it?

    While inefficient for large datasets (O(n²)), it introduces the concept of comparison-based sorting and swapping, which are foundational for more complex algorithms.

    When to reach for this

    • When the input is very small (e.g., N < 50)
    • When the input is nearly sorted (efficient with early termination)
    • When simplicity of implementation is the top priority

    Interview Questions

    Google
    Apple
    Amazon
    Microsoft
    Facebook
    Bloomberg
    Airbnb
    Microsoft

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required