I have an assignment where I have to find the next permutation of a number (without using the function)
In class we figured out how to do this using arrays and swapping around the positions. Unfortunately I'm not sure how to put this into code.
The steps are
- Store the n digits (as current permutation) in an array from left to right
- Find from right to left (pos1) the position of the first digit that breaks the increasing order of the digits
- Find (pos2) to right of pos1, pos2 is the position of the immediate digit greater than the digit in pos1
- Swap the two digits in pos1, and pos2
- Inverse the ordered sequence to the right of pos1
- Return the array of n digits as the next permutation to the given one.
This doesn't seem hard but I am drawing blanks. I have no idea how to do anything past the initial reading the numbers into the array for the original permutation