Bubble sort
Computer Science · Algorithms
Question 1 of 10
0Bubble sort (ascending) on [11, 14, 6, 9, 39]. Each pass sweeps left to right over the unsorted part, comparing every adjacent pair and swapping them when the left one is larger. Every pass runs to the end (no early stop). What is the value at index 2 (counting from 0) after pass 1?
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
Bubble sort explained
Bubble sort puts a list into order by repeatedly comparing neighbouring items and swapping any pair that is in the wrong order. Each full pass carries the largest unsorted item to the end of the list, like a bubble rising to the surface.
The algorithm works like this:
- Compare the first two items and swap them if the first is larger.
- Move one place along and repeat until the end of the list. The largest item is now in the last place.
- Make another pass over the unsorted part, which is one item shorter each time.
- Stop when a complete pass makes no swaps, because the list is sorted.
For a list of items, bubble sort can need up to passes, each with several comparisons. This gives a worst-case time complexity of , so it is slow for large lists. If the list is already sorted, the early stop in step 4 ends it after one pass, which is .
A common mistake is to think one pass sorts the whole list. A single pass only moves the largest item to the end, so several passes are usually needed.
Worked example
Sort [5, 1, 4, 2] into ascending order using bubble sort.
- Pass 1: swap 5 and 1, then 5 and 4, then 5 and 2, giving [1, 4, 2, 5]. That is 3 comparisons and 3 swaps.
- Pass 2: 1 and 4 are in order, so swap 4 and 2 to give [1, 2, 4, 5]. That is 2 comparisons and 1 swap.
- Pass 3: only 1 and 2 are compared, and they are in order, so there are no swaps and the sort stops.
- In total the sort makes 6 comparisons and 4 swaps.
Key facts
- The practice questions on this page always run full passes. They do not stop early, even if the list is already sorted. They tell you so in each question.
- In pass the sweep compares the pairs up to the last unsorted position. That is comparisons.
- So the total number of comparisons is always , whatever the input.
- Each swap fixes exactly one pair that was out of order, so the total number of swaps equals the number of out-of-order pairs in the original list.
- The short explainer above stops when a pass makes no swaps. That is the early-stop version. Always follow the rule in the question.
Worked example 2
Trace bubble sort (ascending) on [6, 3, 8, 2, 5] with no early stop. Give the comparisons, swaps and list after each pass.
- Pass 1 (4 comparisons, 3 swaps): 6 and 3 swap, 6 and 8 stay, 8 and 2 swap, 8 and 5 swap. List: [3, 6, 2, 5, 8]
- Pass 2 (3 comparisons, 2 swaps): 3 and 6 stay, 6 and 2 swap, 6 and 5 swap. List: [3, 2, 5, 6, 8]
- Pass 3 (2 comparisons, 1 swap): 3 and 2 swap, 3 and 5 stay. List: [2, 3, 5, 6, 8]
- Pass 4 (1 comparison, 0 swaps): 2 and 3 stay. List: [2, 3, 5, 6, 8]
Totals: 10 comparisons and 6 swaps. Check: . The list was sorted after pass 3, but pass 4 still runs because there is no early stop.
Worked example 3
How many swaps are made in total on [2, 4, 1, 3, 5, 6], and how many passes would be needed with an early stop?
- Out-of-order pairs: (2, 1), (4, 1) and (4, 3). That is 3, so we expect 3 swaps.
- Pass 1 (5 comparisons, 2 swaps): 4 and 1 swap, then 4 and 3 swap. List: [2, 1, 3, 4, 5, 6]
- Pass 2 (4 comparisons, 1 swap): 2 and 1 swap. List: [1, 2, 3, 4, 5, 6]
- Pass 3 (3 comparisons, 0 swaps), pass 4 (2 comparisons, 0 swaps) and pass 5 (1 comparison, 0 swaps): nothing changes.
- Total swaps: , which matches the count of out-of-order pairs.
- Total comparisons without early stop: .
- With an early stop the sort ends after pass 3, the first pass with no swaps. That is 3 passes and comparisons.
Common mistakes
- Stopping when the list looks sorted. If the question says no early stop, you must still count the remaining passes. They add comparisons but no swaps.
- Comparing the full list in every pass. Each pass is one pair shorter, because the largest items are already at the end.
- Counting comparisons as swaps. A comparison happens every time, and a swap only when the left item is bigger.
- Swapping equal neighbours. Swap only when the left item is strictly larger. The practice lists have distinct values, so this rarely matters there.
- Saying one pass sorts the list. One pass only guarantees that the largest unsorted item reaches the end.
Exam tips
- Write the list after every pass, in a column. Errors then stay local, and you can recover marks for method.
- Keep the index convention in view: the first item is at index 0 if the question counts from 0.
- To check your swap total, count the out-of-order pairs in the starting list. The two numbers must agree.
- Describe the complexity with care: worst case . With an early stop, an already sorted list needs one pass, so .
- A reversed list is the worst case: with 6 items it makes 15 swaps.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
Bubble sort (ascending) on [24, 16, 21, 3, 40]. Each pass sweeps left to right over the unsorted part, comparing every adjacent pair and swapping them when the left one is larger. Every pass runs to the end (no early stop). After pass 1, which of these values is in its final sorted position?
- 16
- 21
- 40
- 3
Medium
Bubble sort (ascending) on [36, 5, 31, 12, 13]. Each pass sweeps left to right over the unsorted part, comparing every adjacent pair and swapping them when the left one is larger. Every pass runs to the end (no early stop). What is the value at index 4 (counting from 0) after pass 2?
Hard
Bubble sort (ascending) on [24, 31, 14, 33, 7, 6, 9]. Each pass sweeps left to right over the unsorted part, comparing every adjacent pair and swapping them when the left one is larger. Every pass runs to the end (no early stop). How many swaps are made in passes 1 to 4 in total?