A simple sorting algorithm that repeatedly walks through the list, compares each pair of adjacent elements, and swaps them if they are in the wrong order. This process repeats until no more swaps are needed, meaning the list is fully sorted.
Heavier bubbles sink and lighter bubbles rise โ larger values slowly move to the end of the list.
Showing a random 20 of 50 problems.
Example 1
medium
Bubble sort [1,3,2,4] ascending with early-exit. Passes and swaps?[1, 3, 2, 4] โ one out-of-order adjacent pair at positions 1 and 2
Example 2
medium
Fully bubble sort [4,2,3,1] ascending. Give the list after each pass and the total swaps.Initial state before fully sorting [4, 2, 3, 1] ascending
Example 3
easy
If a bubble sort pass makes zero swaps, what does that tell you?
Example 4
hard
A list of 1 million records takes ~1s with merge sort. Roughly how long with bubble sort, assuming same per-op cost?
Example 5
easy
After k passes, why can you skip comparing the last k elements?
Example 6
challenge
Prove that bubble sort's worst-case swap count is exactly n(nโ1)/2.
Example 7
easy
True or false: bubble sort is a STABLE sorting algorithm.
Example 8
medium
How many comparisons does unoptimized bubble sort do on a list of 5 (sum over all passes)?
Example 9
hard
Why does bubble-sort swap count equal the inversion count of the input?
Example 10
easy
Do one pass of ascending bubble sort on [5,2,4].Start of one ascending bubble-sort pass on [5, 2, 4]
Example 11
easy
Bubble sort compares which elements?
Example 12
easy
On a list of 6 elements, how many adjacent comparisons does the first pass make?
Example 13
hard
On a list where every element is one position away from its sorted spot, how many swaps does bubble sort make?
Example 14
easy
On a list of 7, how many adjacent comparisons does the FIRST pass make?
Example 15
medium
Fully bubble sort [2,4,1] (ascending). Give the final list and total number of swaps.Initial state before fully sorting [2, 4, 1] ascending
Example 16
easy
How does bubble sort decide whether to swap two adjacent elements?
Example 17
easy
Do one pass of ascending bubble sort on [3,1,2].Start of one ascending bubble-sort pass on [3, 1, 2]
Example 18
medium
Why might bubble sort still be taught despite being slow?
Example 19
easy
After one full pass of ascending bubble sort, which element reaches its correct final position?
Example 20
medium
An array [2,2,1] is sorted with bubble sort. After sorting, do the two 2's keep their original relative order? Why or why not?[2, 2, 1] โ do the two equal 2's keep their relative order after sorting?