Sattolo cycle: Difference between revisions

Added Algol 68
(Undo revision 326199 by Tigerofdarkness (talk))
(Added Algol 68)
Line 69:
[3, 8, 9, 2, 1, 5, 4, 10, 7, 6]
[2, 9, 7, 5, 1, 3, 8, 10, 6, 4]
</pre>
 
=={{header|ALGOL 68}}==
Arrays in Algol 68 need not have a lower bound of 0, other than that, this implements the pseudo code.
<lang algol68>BEGIN
# reorders the elements of a using the Sattalo cycle #
# this operates on integer arrays, additional SATTALO operators #
# could be defined for other types #
# a is returned so we can write e.g. SATTALO SATTALO a to cycle #
# the elements twice #
OP SATTALO = ( REF[]INT a )REF[]INT:
BEGIN
REF[]INT aa := a[ @ 0 ];
FOR i FROM UPB aa BY -1 TO 1 DO
INT j = ENTIER ( next random * i );
INT t = aa[ i ];
aa[ i ] := aa[ j ];
aa[ j ] := t
OD;
a
END # SATTALO # ;
[ 1 : 10 ]INT a := []INT( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )[ @ 1 ];
TO 5 DO
SATTALO a;
FOR i FROM LWB a TO UPB a DO print( ( " ", whole( a[ i ], -3 ) ) ) OD;
print( ( newline ) )
OD
END</lang>
{{out}}
<pre>
4 9 2 5 3 1 8 10 7 6
6 2 10 7 5 3 4 8 9 1
1 4 3 2 10 6 5 7 8 9
5 7 1 9 6 4 8 2 10 3
4 10 5 6 3 8 7 1 9 2
</pre>
 
3,044

edits