RosettaCodeData/Task/Permutations/TAV/permutations-4.tav

45 lines
1.1 KiB
Plaintext

\( Two arrays, iterative, not in lexical order.
Input row (a) provides for each element where it was inserted last.
\)
\C Publication syntax
permute iterative (n):
a = new row size n \ state of last number
r = new row size n \ output row
l = 1
while l > 0 \ need to start over
\ find the key of a void element
k = 0 \ if not found
for j = from a[l] + 1 upto n
if r[j] == () \ void in result
k = j \ use it
continue
\ evaluate
if k != 0
\ key for void cell to be set
r[k] = l
a[l] = k
if l == n
\ permuation complete
print join r
\ backup
k = a[l]
r[k] = ()
continue
l += 1
continue \ next level
\ no void cell found
if l == 1
break \ done, no more permutations
\ backup
a[l] = 0
l -= 1
k = a[l]
r[k] = ()
continue
\( command line parameter: number of elements
\)
main (parms):+
n =: string parms[1] as integer else 3
p =: permute iterative n