I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Category talk:ALGOL 68-rows

From Rosetta Code

Source code[edit]

# rows.incl.a68: array related utilities for Algol 68 RC tasks                #
 
# prints the elements of an array of integers separated by spaces #
OP SHOW = ( []INT list )VOID:
FOR i FROM LWB list TO UPB list DO
print( ( " ", whole( list[ i ], 0 ) ) )
OD # SHOW # ;
# prints the elements of an array of reals separated by spaces #
OP SHOW = ( []REAL list )VOID:
FOR i FROM LWB list TO UPB list DO
print( ( " ", fixed( list[ i ], -14, 8 ) ) )
OD # SHOW # ;
 
# operators and modes to allow "QUICKSORT x FROMELEMENT lb TOELEMENT ub" #
 
# mode to hold the lower and upper element indexes to sort #
MODE SORTBOUNDS = STRUCT( INT lb, ub );
 
# unary operator that returns its argument #
# if we were to support multiple sort methods, could retuen the array #
# plus a code to specify sorting using quicksort #
OP QUICKSORT = ( REF[]INT a )REF[]INT: a;
OP QUICKSORT = ( REF[]REAL a )REF[]REAL: a;
 
# constructs a SORTBOUNDS from its parameters #
PRIO TOELEMENT = 9;
OP TOELEMENT = ( INT lb, ub )SORTBOUNDS: SORTBOUNDS( lb, ub );
 
# in-place quick sort an array #
PRIO FROMELEMENT = 8;
# in-place quick sort an array of integers #
OP FROMELEMENT = ( REF[]INT a, SORTBOUNDS bounds )REF[]INT:
IF INT lb = lb OF bounds;
INT ub = ub OF bounds;
ub <= lb
THEN
# empty array or only 1 element #
a
ELSE
# more than one element, so must sort #
INT left := lb;
INT right := ub;
# choosing the middle element of the array as the pivot #
INT pivot := a[ left + ( ( right + 1 ) - left ) OVER 2 ];
WHILE
WHILE IF left <= ub THEN a[ left ] < pivot ELSE FALSE FI
DO
left +:= 1
OD;
WHILE IF right >= lb THEN a[ right ] > pivot ELSE FALSE FI
DO
right -:= 1
OD;
left <= right
DO
INT t := a[ left ];
a[ left ] := a[ right ];
a[ right ] := t;
left +:= 1;
right -:= 1
OD;
QUICKSORT a FROMELEMENT lb TOELEMENT right;
QUICKSORT a FROMELEMENT left TOELEMENT ub;
a
FI # FROMELEMENT # ;
# in-place quick sort an array of reals #
OP FROMELEMENT = ( REF[]REAL a, SORTBOUNDS bounds )REF[]REAL:
IF INT lb = lb OF bounds;
INT ub = ub OF bounds;
ub <= lb
THEN
# empty array or only 1 element #
a
ELSE
# more than one element, so must sort #
INT left := lb;
INT right := ub;
# choosing the middle element of the array as the pivot #
REAL pivot := a[ left + ( ( right + 1 ) - left ) OVER 2 ];
WHILE
WHILE IF left <= ub THEN a[ left ] < pivot ELSE FALSE FI
DO
left +:= 1
OD;
WHILE IF right >= lb THEN a[ right ] > pivot ELSE FALSE FI
DO
right -:= 1
OD;
left <= right
DO
REAL t := a[ left ];
a[ left ] := a[ right ];
a[ right ] := t;
left +:= 1;
right -:= 1
OD;
QUICKSORT a FROMELEMENT lb TOELEMENT right;
QUICKSORT a FROMELEMENT left TOELEMENT ub;
a
FI # FROMELEMENT # ;
 
 
# END rows.incl.a68 #