Last list item: Difference between revisions

ALGOL 68 →‎With sorting: Use new ALGOL 68-rows library
(add BQN)
(ALGOL 68 →‎With sorting: Use new ALGOL 68-rows library)
Line 12:
===With sorting===
{{trans|Wren}}
{{libheader|ALGOL 68-rows}}
<lang algol68>BEGIN # find the last element after repeatedely adding the sum #
# of the two smallest elements and removing them #
PR read "rows.incl.a68" PR # row related utilities #
# Quicksorts in-place the array of integers a, from lb to ub #
PROC quicksort = ( REF[]INT a, INT lb, ub )VOID:
IF ub > lb THEN
# 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, lb, right );
quicksort( a, left, ub )
FI # quicksort # ;
 
[ 1 : 9 ]INT a := ( 6, 81, 243, 14, 25, 49, 123, 69, 11 );
INT a count := UPB a;
WHILE a count > 1 DO
quicksort(QUICKSORT a, FROMELEMENT LWB a, TOELEMENT a count );
print( ( "Sorted list:" ) );FOR i TO a count DO print( ( " ", whole( a[ i ], 0 ) ) ) OD;
INT sum = a[ 1 ] + a[ 2 ];
3,028

edits