I have a random collection of 5 unique integers ranging from 1 to 15 that I want to sort in ascending order from right to left.
Example input: 15 6 7 3 4
Desired output: 15 7 6 4 3
Rules:
We can only "see" the three rightmost integers at a time (7 3 4) in (15 6 7 3 4) and can only take actions on the rightmost integer in the array (4). We can also use up to 5 integer variables in the code.
Possible actions:
putend, the rightmost integer is placed at the leftmost position of the array
(15 6 7 3 4) -> (4 15 6 7 3)
swap, the rightmost integer is swapped with the second integer. Rightmost integer is moved one step left in the array.
(15 6 7 3 4) -> ( 15 6 7 4 3)
double swap, the rightmost integer is swapped with second element and then swapped with the third element. Rightmost integer is moved 2 steps left in the array.
(15 6 7 3 4) -> (15 6 4 7 3).
My attempt:
while (not solved)
if first element < 2nd element
putend
else if first element > 2nd element
if first element > 3rd element
double swap
else
swap
Output:
15 6 7 3 4 swap
15 6 7 4 3 putend
3 15 6 7 4 putend
4 3 15 6 7 swap
4 3 15 7 6 putend
6 4 3 15 7 putend
7 6 4 3 15 (problem here, triggers double swap but want putend and somehow detect that I am done)