Skip to visualizations

The visualization lab

Explore the gallery

Sorting

Bubble sort

Problem statement

Given an array of n values, arrange them in ascending order.

Efficient solution idea

Compare adjacent values and swap any pair that is out of order. Each pass places the largest remaining value at the end; stop early if a pass makes no swaps.

Loading visualization…

Step-by-step approach
  1. Let n be the number of values. Start with the whole array as the unsorted region.
  2. At the beginning of each pass, set swapped = false and scan adjacent pairs from left to right within the unsorted region.
  3. If A[j] > A[j + 1], swap the two values and set swapped = true. Otherwise, leave the pair unchanged.
  4. After the pass, the largest value in the unsorted region is in its final position. Exclude that position from the next pass.
  5. If the pass made no swaps, stop early. Otherwise, repeat until at most one unsorted value remains.
  6. Return the array in ascending order.

Keep in mind: The suffix after the last compared position is sorted.

O(n²) worst, O(n) best; O(1) auxiliary space.

Pseudocode
bubbleSort(A, n)
    for end from n - 1 down to 1
        swapped = false

        // Compare adjacent values in the unsorted part
        for j from 0 to end - 1
            if A[j] > A[j + 1]
                swap(A[j], A[j + 1])
                swapped = true

        // No swaps means the array is already sorted
        if swapped == false
            break

    return A
Explore edge cases

To explore a case, enter its values in Input settings and press Play.

Already sorted

One pass is enough to detect that no swaps are needed.

1, 2, 3, 4, 5, 6

All equal

Equal neighbors are never swapped.

4, 4, 4, 4, 4, 4
Similar problems

Explore visualizations

Choose a card to view its visualization above.

15 visualizations

Sorting

Bubble sort

Compare neighboring values and watch larger values move right.

O(n²) worst · O(n) bestSelected
Sorting

Insertion sort

Move each key into place within an ordered prefix.

O(n²) worst · O(n) bestExplore
Sorting

Selection sort

Find the minimum remaining value and fix its position.

O(n²) comparisons in every caseExplore
Sorting

Sort 0, 1 and 2

Partition zeros, ones and twos with low, mid and high.

O(n) time · O(1) spaceExplore
Prefix and suffix

Product except self

Combine products to the left and right without division.

O(n) time · O(1) extra beyond outputExplore
Prefix and suffix

Equilibrium index

Find an index with equal sums on both sides.

O(n) time · O(1) spaceExplore
Sorting

Counting sort

Count values, accumulate positions and place them stably.

O(n + k) time · O(n + k) spaceExplore
Two pointers

Container with most water

Move the shorter wall while keeping the largest water area.

O(n) time · O(1) spaceExplore
Two pointers

Pair sum

Find two distinct positions with a target sum in sorted input.

O(n) time · O(1) spaceExplore
Voting

Find majority element

Cancel unequal votes, then verify a strict majority.

O(n) time · O(1) spaceExplore
Sorting

Merge sort

Split into halves and combine values in a stable merge.

O(n log n) timeExplore
Sorting

Quick sort

Partition around a pivot, then sort the two remaining ranges.

O(n log n) average · O(n²) worstExplore