An early-exit bubble sort sorts a list of n = 8 values into ascending order. Pass k compares neighbouring positions from the front up to position 8 − k, swapping any pair whose left value is larger. The algorithm stops after a pass that makes no swaps, or after n − 1 = 7 passes, whichever comes first.
The algorithm is run on two lists:
For each list, give the number of passes performed, the total number of comparisons and the total number of swaps, each as a whole number.
Unlock full course notes by signing up
Tracing two passes
Bubble sort sorts the list 8, 3, 5, 1, 9, 2 into ascending order. Each pass compares adjacent pairs from left to right across the unsorted part and swaps a pair when the left value is larger. Fill in the value at each position after pass 1 and after pass 2, and the number of swaps made during each of those passes.
Total swaps from inversions
Bubble sort sorts the list 3, 7, 2, 6, 1 into ascending order, comparing adjacent pairs from left to right and swapping a pair whenever the left value is larger. How many swaps does it make in total, from the starting list to the fully sorted list? Give your answer as a whole number.
Why the strict comparison keeps ties in order
A teacher sorts three student records by score in ascending order with bubble sort. The records start in the order Lee (85), Kai (72), Mia (72). Bubble sort exchanges two neighbouring records only when the left score is strictly greater than the right score. Pass 1 compares positions 1 and 2, then positions 2 and 3. Pass 2 compares only positions 1 and 2, because the largest record has already settled at the end. The algorithm stops after a pass that makes no exchanges or after 2 passes, whichever comes first. Answer each name blank with the student's name only, without the score.
With this strict test, bubble sort makes ________ swaps in total and the sorted list begins with ________. If the test is changed to greater than or equal, with the same pass rules, it makes ________ swaps and the sorted list begins with ________. Because the strict test keeps records with equal keys in their original relative order, bubble sort with that test is called a ________ sort.
Best and worst case work for 50 values
An early-exit bubble sort sorts lists of 50 distinct values into ascending order. Each pass compares adjacent pairs from left to right across the unsorted part, which shrinks by one position after every pass, and the algorithm stops after the first pass that makes no swaps or when no unsorted pairs remain. Give, as whole numbers, the number of comparisons when the list is already in ascending order, the number of comparisons when the list is in descending order, and the number of swaps when the list is in descending order.