Permutations: Difference between revisions
Content deleted Content added
Fixed lazy D version |
|||
Line 123: | Line 123: | ||
-- print all permutations of 1 .. n |
-- print all permutations of 1 .. n |
||
-- where n is given as a command line argument |
-- where n is given as a command line argument |
||
-- to compile with |
-- to compile with GNAT : gnat make perm.adb |
||
-- to call : perm n |
-- to call on command line : perm n |
||
with |
with Ada.Text_IO, Ada.Command_Line; |
||
procedure |
procedure Perm is |
||
use |
use Ada.Text_IO, Ada.Command_Line; |
||
N : Integer; |
|||
begin |
begin |
||
if |
if Argument_Count /= 1 |
||
then |
then |
||
Put_Line (Command_Name & " n (with n >= 1)"); |
|||
return; |
return; |
||
else |
else |
||
N := Integer'Value (Argument (1)); |
|||
end if; |
end if; |
||
declare |
declare |
||
subtype |
subtype Element is Integer range 1 .. N; |
||
type |
type Permutation is array (Element'Range) of Element; |
||
P : Permutation; |
|||
Is_Last : Boolean := False; |
|||
procedure Swap (A, B : in out Integer) is |
|||
⚫ | |||
begin |
|||
⚫ | |||
⚫ | |||
end; |
|||
-- compute next permutation in lexicographic order |
-- compute next permutation in lexicographic order |
||
Line 151: | Line 158: | ||
-- exchange the element x preceding the tail, with the minimum value in the tail, |
-- exchange the element x preceding the tail, with the minimum value in the tail, |
||
-- that is also greater than x |
-- that is also greater than x |
||
procedure |
procedure Next is |
||
I, J, K : Element; |
|||
begin |
begin |
||
-- find longest tail decreasing sequence |
-- find longest tail decreasing sequence |
||
Line 158: | Line 165: | ||
-- and the ith element will be exchanged later |
-- and the ith element will be exchanged later |
||
-- with some element of the tail |
-- with some element of the tail |
||
Is_Last := True; |
|||
I := N - 1; |
|||
loop |
loop |
||
if |
if P (I) < P (I+1) |
||
then |
then |
||
Is_Last := False; |
|||
exit; |
exit; |
||
end if; |
end if; |
||
Line 169: | Line 176: | ||
-- next instruction will raise an exception if i = 1, so |
-- next instruction will raise an exception if i = 1, so |
||
-- exit now (this is the last permutation) |
-- exit now (this is the last permutation) |
||
exit when |
exit when I = 1; |
||
I := I - 1; |
|||
end loop; |
end loop; |
||
-- if all the elements of the permutation are in |
-- if all the elements of the permutation are in |
||
-- decreasing order, this is the last one |
-- decreasing order, this is the last one |
||
if |
if Is_Last then |
||
return; |
return; |
||
end if; |
end if; |
||
-- sort the tail, i.e. reverse it, since it is in decreasing order |
-- sort the tail, i.e. reverse it, since it is in decreasing order |
||
J := I + 1; |
|||
K := N; |
|||
while |
while J < K loop |
||
Swap (P (J), P (K)); |
|||
J := J + 1; |
|||
K := K - 1; |
|||
⚫ | |||
⚫ | |||
end loop; |
end loop; |
||
-- find lowest element in the tail greater than the ith element |
-- find lowest element in the tail greater than the ith element |
||
J := N; |
|||
while |
while P (J) > P (I) loop |
||
J := J - 1; |
|||
end loop; |
end loop; |
||
J := J + 1; |
|||
-- exchange them |
-- exchange them |
||
-- this will give the next permutation in lexicographic order, |
-- this will give the next permutation in lexicographic order, |
||
-- since every element from ith to the last is minimum |
-- since every element from ith to the last is minimum |
||
Swap (P (I), P (J)); |
|||
p (i) := p (j); |
|||
⚫ | |||
end next; |
end next; |
||
procedure |
procedure Print is |
||
begin |
begin |
||
for |
for I in Element'Range loop |
||
Put (Integer'Image (P (I))); |
|||
end loop; |
end loop; |
||
New_Line; |
|||
end |
end Print; |
||
-- initialize the permutation |
-- initialize the permutation |
||
procedure |
procedure Init is |
||
begin |
begin |
||
for |
for I in Element'Range loop |
||
P (I) := I; |
|||
end loop; |
end loop; |
||
end |
end Init; |
||
begin |
begin |
||
Init; |
|||
loop |
while not Is_Last loop |
||
Print; |
|||
Next; |
|||
exit when is_last; |
|||
end loop; |
end loop; |
||
end; |
end; |
||
end |
end Perm;</lang> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |