For example, if we want to generate the permutations of 1,2,3,4. First we find the longest non-increasing suffix (with least length 2). That is 3,4. The smallest element in the suffix larger than 3 is 4, then we swap 3,4 and reverse the elements after 4 (that is 3 itself). Then we get 1,2,4,3
Now the longest non-increasing suffix is 2,4,3. The smallest element in the suffix larger than 2 is 3, then we swap 2,3 and we get 1,3,4,2. Afterwards we reverse the elements 4,2, and we get 1,3,2,4
Next Combination in Lexicographic Order
For example, if we want to find the next 4-combination of 1,2,3,4,5,6 after 1,2,5,6. The rightmost element that can be incremented is 2, so we get 1,3,5,6. Then we need to set all the elements to the right to the smallest values possible, that is 1,3,4,5