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

25 lines
727 B
Plaintext

\( Calculate all permutations of integers 1..n
General variant: simple, single array, recursive, not in lexical order
A row with n places is used, initially 0.
The current element l replaces the free places one by one,
and the next element l+1 is probed with the array.
\)
\C Use publication mode with keywords instead of symbols
permute1 (l) to (n) fill (r):
if l > n
print join r \ output requires most of CPU time
return
for j = from 1 upto n
if r[j] == 0
r[j] = l
permute1 l+1 to n fill r
r[j] = 0
\( command line parameter is number of elements
\)
main (parms):+
n = string parms[1] as integer else 3
r = new row size n init 0
permute1 1 to n fill r