Bubble Sort Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Bubble Sort.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

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.

Read the full concept explanation โ†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Each pass moves the largest unsorted element to its correct position at the end.

Common stuck point: Bubble sort is O(nยฒ) โ€” avoid it for large data; use merge sort or quick sort instead.

Sense of Study hint: To trace bubble sort, make repeated passes through the list. On each pass, compare every pair of adjacent elements and swap if the left is greater. After each pass, the largest unsorted value is in its final position. Stop when a full pass makes no swaps.

Worked Examples

Example 1

medium
Fully bubble sort [4,2,3,1][4, 2, 3, 1] ascending. Give the list after each pass and the total swaps.

Answer

[1,2,3,4];ย 5ย swaps[1, 2, 3, 4];\ 5\text{ swaps}

First step

1
Pass 1: [4,2,3,1]โ†’[2,4,3,1]โ†’[2,3,4,1]โ†’[2,3,1,4][4,2,3,1] \to [2,4,3,1] \to [2,3,4,1] \to [2,3,1,4] (3 swaps).

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan โ€” every worked solution, all subjects

Example 2

medium
Bubble sort [1,3,2,4][1, 3, 2, 4] ascending with early-exit. Passes and swaps?

Example 3

medium
Bubble sort [5,1,4,2,8][5, 1, 4, 2, 8] ascending. Final list and total swap count?

Example 4

hard
Trace bubble sort with early-exit on [3,2,1,4][3, 2, 1, 4]. Give passes, swaps per pass, and total work.

Example 5

hard
Bubble sort [5,4,3,2,1][5, 4, 3, 2, 1] descending (largest first). Final list and swap count?

Example 6

hard
Write pseudocode for bubble sort with both optimizations: early-exit and skip-last-kk.

Example 7

challenge
Cocktail (bidirectional bubble) sort alternates left-to-right and right-to-left passes. On [5,1,4,2,8,0][5, 1, 4, 2, 8, 0], does it finish faster than standard bubble sort? Sketch the argument.

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
What is bubble sort's time complexity?

Example 2

easy
Bubble sort compares which elements?

Example 3

easy
After one full pass of ascending bubble sort, which element reaches its correct final position?

Example 4

easy
Do one pass of ascending bubble sort on [3,1,2][3, 1, 2].

Example 5

easy
If a bubble sort pass makes zero swaps, what does that tell you?

Example 6

easy
Bubble sort's best case (already sorted, with early exit) is what complexity?

Example 7

easy
How many adjacent comparisons does the FIRST pass make on a list of 55?

Example 8

easy
After kk passes, why can you skip comparing the last kk elements?

Example 9

medium
Fully bubble sort [2,4,1][2, 4, 1] (ascending). Give the final list and total number of swaps.

Example 10

medium
How many comparisons does unoptimized bubble sort make on a list of 44 (sum over all passes)?

Example 11

medium
On a reverse-sorted list [3,2,1][3,2,1], how many swaps does bubble sort make in total?

Example 12

medium
Why is bubble sort a poor choice for sorting a million records?

Example 13

medium
Without the last-kk optimization, bubble sort compares into the already-sorted tail. What does this waste?

Example 14

medium
Is bubble sort stable, and what makes it so?

Example 15

medium
Bubble sort [1,2,3,5,4][1,2,3,5,4] with early exit. How many passes until it stops?

Example 16

medium
Fully bubble sort [3,1,2][3, 1, 2] ascending. Give the final list and total swaps.

Example 17

medium
How many comparisons does unoptimized bubble sort do on a list of 55 (sum over all passes)?

Example 18

challenge
Prove bubble sort's worst case does n(nโˆ’1)2\frac{n(n-1)}{2} comparisons. Tie it to the number of inversions.

Example 19

challenge
Compare bubble sort and merge sort on (a) already-sorted and (b) reverse-sorted input of size nn, in Big-O. When might bubble sort actually be preferable?

Example 20

challenge
A list has exactly 2 inversions. How many swaps will bubble sort make, and why does swap count equal inversion count?

Example 21

easy
Do one pass of ascending bubble sort on [5,2,4][5, 2, 4].

Example 22

easy
On a list of 66 elements, how many adjacent comparisons does the first pass make?

Example 23

easy
True or false: bubble sort is a STABLE sorting algorithm.

Example 24

easy
After two complete passes of ascending bubble sort, which positions are guaranteed sorted?

Example 25

easy
Sort [2,1][2, 1] with bubble sort. Number of swaps?

Example 26

easy
How does bubble sort decide whether to swap two adjacent elements?

Example 27

easy
On a list of 77, how many adjacent comparisons does the FIRST pass make?

Example 28

medium
How many comparisons does unoptimized bubble sort make on a list of 66 (sum over passes)?

Example 29

medium
On reverse-sorted [4,3,2,1][4,3,2,1], how many bubble sort swaps total?

Example 30

medium
Why does early-exit optimization make sorted input O(n)O(n)?

Example 31

medium
Compare bubble sort and merge sort on n=10,000n=10{,}000. Approximate ratio of comparisons.

Example 32

medium
After kk passes of bubble sort on nn items, what range still needs comparing in the next pass?

Example 33

medium
How many comparisons does unoptimized bubble sort do on n=10n=10?

Example 34

medium
Without the skip-last-kk optimization, what is wasted on later passes?

Example 35

medium
Why might bubble sort still be taught despite being slow?

Example 36

medium
An array [2,2,1][2, 2, 1] is sorted with bubble sort. After sorting, do the two 2's keep their original relative order? Why or why not?

Example 37

hard
Why does bubble-sort swap count equal the inversion count of the input?

Example 38

hard
A list of 11 million records takes ~1s with merge sort. Roughly how long with bubble sort, assuming same per-op cost?

Example 39

hard
On a list where every element is one position away from its sorted spot, how many swaps does bubble sort make?

Example 40

hard
Bubble sort is sometimes called 'sinking sort' if implemented differently. What does the alternate variant do?

Example 41

challenge
Prove that bubble sort's worst-case swap count is exactly n(nโˆ’1)/2n(n-1)/2.

Background Knowledge

These ideas may be useful before you work through the harder examples.

sortingiterationefficiency