Permutations: Difference between revisions

no edit summary
No edit summary
Line 4,162:
4 3 2 1
</pre>
 
=={{header|FutureBasic}}==
Here's a sweet recursive algorithm, adapted from Robert Sedgewic's 'Algorithms' (1989, p. 628). It generates its own array of integers.
<syntaxhighlight lang="futurebasic">
 
begin globals
Short w( 4 ), i = -1
end globals
 
void local fn perm( k as Short)
Short j
i ++ : w( k ) = i
if i = 4
for j = 1 to 4 : print w( j ),
next : print
else
for j = 1 to 4 : if w( j ) = 0 then fn perm( j )
next
end if
i -- : w( k ) = 0
end fn
 
fn perm(0)
 
handleevents
 
</syntaxhighlight>
 
=={{header|GAP}}==
60

edits