Bubble sort

Show the array after exactly one full pass of bubble sort, starting on the left.

Examples

Example 1

Input
[4, 2, 6, 1]
Expected output
[2, 4, 1, 6]

Example 2

Input
[1, 2, 3]
Expected output
[1, 2, 3]

Hints and solution

Hint 1

Compare the current pair even after a swap. Move the starting index of the next pair one place right.

Reveal worked answer

For [4, 2, 6, 1], swap 4 and 2, keep 4 and 6, then swap 6 and 1. Only 6 is guaranteed to be in its final position after this pass.

Back to the lesson