Sattolo cycle: Difference between revisions

1,327 bytes removed ,  3 years ago
Undo revision 326199 by Tigerofdarkness (talk)
(Added Algol 68)
(Undo revision 326199 by Tigerofdarkness (talk))
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}}==
The lower bound of an array need not be 0 (or 1) in Algol 68, other than that, this is as per 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
INT len = ( UPB a - LWB a ) + 1;
FOR i FROM UPB a BY -1 TO LWB a + 1 DO
INT j = ENTIER ( ( next random * len ) + LWB a );
INT t = a[ i ];
a[ i ] := a[ j ];
a[ j ] := t
OD;
a
END # SATTALO # ;
[ 1 : 10 ]INT a := ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
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>
8 3 4 2 6 5 10 7 1 9
8 9 3 6 4 10 1 5 2 7
2 9 10 3 6 4 1 7 5 8
2 4 1 9 3 7 5 8 10 6
6 10 2 1 8 7 4 9 5 3
</pre>
 
3,038

edits