Swap Adjacent Inversions in One Pass
Problem statement
Make exactly one left-to-right pass over a copy of values. At index i, if the current value is greater than the current value at i + 1, swap that adjacent pair. Then continue to index i + 1 using the updated array.
Return the array after the pass.
Function
swapAdjacentInversions(values: int[]) → int[]Examples
Example 1
values = [4,3,2,1]return = [3,2,1,4]The large first value bubbles right through the entire pass.
Example 2
values = [1,2,3]return = [1,2,3]No adjacent inversion changes the array.
Constraints
0 <= values.length <= 100000-1000000000 <= values[i] <= 1000000000