Anonymous user
Permutations: Difference between revisions
m
→{{header|Ada}}
(Fixed lazy D version) |
m (→{{header|Ada}}) |
||
Line 123:
-- print all permutations of 1 .. n
-- where n is given as a command line argument
-- to compile with
-- to call on command line : perm n
with
procedure
use
begin
if
then
return;
else
end if;
declare
subtype
type
procedure Swap (A, B : in out Integer) is
begin
end;
-- compute next permutation in lexicographic order
Line 151 ⟶ 158:
-- exchange the element x preceding the tail, with the minimum value in the tail,
-- that is also greater than x
procedure
begin
-- find longest tail decreasing sequence
Line 158 ⟶ 165:
-- and the ith element will be exchanged later
-- with some element of the tail
loop
if
then
exit;
end if;
Line 169 ⟶ 176:
-- next instruction will raise an exception if i = 1, so
-- exit now (this is the last permutation)
exit when
end loop;
-- if all the elements of the permutation are in
-- decreasing order, this is the last one
if
return;
end if;
-- sort the tail, i.e. reverse it, since it is in decreasing order
while
▲ j := j + 1;
▲ k := k - 1;
end loop;
-- find lowest element in the tail greater than the ith element
while
end loop;
-- exchange them
-- this will give the next permutation in lexicographic order,
-- since every element from ith to the last is minimum
▲ p (j) := t;
end next;
procedure
begin
for
end loop;
end
-- initialize the permutation
procedure
begin
for
end loop;
end
begin
while not Is_Last loop
end loop;
end;
end
=={{header|ALGOL 68}}==
|