### SLA_PERMUT

Next Permutation

ACTION:
Generate the next permutation of a specified number of items.
CALL:
CALL sla_PERMUT (N, ISTATE, IORDER, J)
##### GIVEN:
 N I number of items: there will be N! permutations ISTATE I(N) state, ISTATE(1)$=-1$ to initialize

##### RETURNED:
 ISTATE I(N) state, updated ready for next time IORDER I(N) next permutation of numbers 1,2,…,N J I status: $-$1 = illegal N (zero or less is illegal) 0 = OK $+$1 = no more permutations available

NOTES:
(1)
This routine returns, in the IORDER array, the integers 1 to N inclusive, in an order that depends on the current contents of the ISTATE array. Before calling the routine for the first time, the caller must set the first element of the ISTATE array to $-1$ (any negative number will do) to cause the ISTATE array to be fully initialized.
(2)
The first permutation to be generated is:

IORDER(1)=N, IORDER(2)=N-1, ..., IORDER(N)=1

This is also the permutation returned for the “finished” (J=1) case. The final permutation to be generated is:

IORDER(1)=1, IORDER(2)=2, ..., IORDER(N)=N

(3)
If the “finished” (J=1) status is ignored, the routine continues to deliver permutations, the pattern repeating every N!  calls.