Practice

Bubble Sort Pass Counter

2023/Oct/Nov·Variant 2
MEDIUMAlgorithms

Sort 6 integers into ascending order using bubble sort, and count how many swaps the algorithm performs. Output the sorted array (one value per line), then the swap count on the final line.

Input: 6 integers. Output: The 6 sorted values, one per line, followed by the number of swaps.

Example:

Input:  5
        3
        8
        4
        2
        7
Output: 2
        3
        4
        5
        7
        8
        8
Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: Mixed values
Inputs: 5, 3, 8, 4, 2, 7
Expected: 2 3 4 5 7 8 8
Test 2: Already sorted — no swaps
Inputs: 1, 2, 3, 4, 5, 6
Expected: 1 2 3 4 5 6 0
Test 3: Reverse sorted — maximum swaps
Inputs: 6, 5, 4, 3, 2, 1
Expected: 1 2 3 4 5 6 15