RosettaCodeData/Task/Knuth-shuffle/PL-I/knuth-shuffle-2.pli
2014-01-17 05:34:36 +00:00

23 lines
961 B
Text

kn: Proc Options(main);
/*--------------------------------------------------------------------
* 07.01.2014 Walter Pachl translated from REXX version 2
* Iteration i: only the first i elements are candidates for swapping
*-------------------------------------------------------------------*/
Dcl T(10) Bin Fixed(15) Init(1,2,3,4,5,6,7,8,9,10);
Dcl (i,j,temp) Bin Fixed(15) init(0);
Dcl h Char(6);
Call show('In',10); /* show start */
do i = 10 To 2 By -1; /* shuffle */
j=random()*i+1;
Put string(h)Edit(i,j)(f(2),f(3));
temp=t(i); t(i)=t(j); t(j)=temp; /* t(i) <-> t(j) */
Call show(h,i); /* show intermediate states */
end;
Call show('Out',10); /* show final state */
show: Proc(txt,n);
Dcl txt Char(*);
Dcl n Bin Fixed(15);
Put Edit(txt,(t(k) do k=1 To n))(Skip,a(7),10(f(3)));
End;
end;