Last list item: Difference between revisions

→‎{{header|ALGOL 68}}: Updated to revised task requirements
m (add REXX)
(→‎{{header|ALGOL 68}}: Updated to revised task requirements)
Line 10:
 
=={{header|ALGOL 68}}==
Was a translation of the sorted Wren version but the sorting has been removed as per the revised task requirements.
{{Trans|Wren}}
<lang algol68>BEGIN # find the last element after repeatedely adding the sum #
# of the two smallest elements and removing them #
# 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
quicksortprint( ( "List:" ) );FOR i TO a count DO print( ( " ", LWBwhole( a[ i ], a0 count) ) ) OD;
INT left :=s1pos, lbs2pos;
print( ( "Sorted list:" ) );FOR i TO a count DO print( ( " ", whole( a[ i ], 0 ) ) ) OD;
print(IF (a[ newline1 )] );< a[ 2 ]
INTTHEN sums1pos := a[ 1; ]s2pos + a[:= 2 ];
print(ELSE (s1pos "Two smallest:= "2; ,s2pos whole( a[:= 1 ], 0 )
WHILEFI;
, " + ", whole( a[ 2 ], 0 )
FOR i FROM 3 TO a count , " = ", whole( sum, 0 )DO
IF a[ i ] < a[ s1pos ] THEN s2pos := s1pos; s1pos := i
ELIF a[ left i ] :=< a[ rights2pos ]; THEN s2pos := i
IF ub > lb THENFI
DOOD;
INT tsum = a[ s1pos ] :=+ a[ left s2pos ];
print( ( "Sorted; listtwo smallest: " ), );FOR i TOwhole( a[ counts1pos DO], print(0 (), " @ ", whole( a[ i ]s1pos, 0 ) ) ) OD;
, " +and ", whole( a[ 2s2pos ], 0 ), " @ ", whole( s2pos, 0 )
, left"; sum <= right", whole( sum, 0 )
, newline
)
);
a[ 2s1pos ] := sum;
a[FOR 1i :FROM s2pos TO a count - 1 DO a[ i ] := a[ 2i :+ a1 count] ]OD;
a count -:= 1
OD;
Line 57 ⟶ 42:
{{out}}
<pre>
Sorted listList: 6 1181 243 14 25 49 123 69 8111; 123two 243smallest: 6 @ 1 and 11 @ 9; sum = 17
TwoList: 17 81 243 14 25 49 123 69; two smallest: 614 +@ 114 =and 17 @ 1; sum = 31
List: 81 243 31 25 49 123 69; two smallest: 25 @ 4 and 31 @ 3; sum = 56
Sorted list: 14 17 25 49 69 81 123 243
TwoList: 81 243 56 49 123 69; two smallest: 1449 @ 4 and 56 @ +3; 17sum = 31105
List: 81 243 105 123 69; two smallest: 69 @ 5 and 81 @ 1; sum = 150
Sorted list: 25 31 49 69 81 123 243
TwoList: 243 105 123 150; two smallest: 25105 @ 2 and 123 @ +3; 31sum = 56228
List: 243 228 150; two smallest: 150 @ 3 and 228 @ 2; sum = 378
Sorted list: 49 56 69 81 123 243
TwoList: 243 378; two smallest: 49243 @ 1 and 378 @ +2; 56sum = 105621
Sorted list: 69 81 105 123 243
Two smallest: 69 + 81 = 150
Sorted list: 105 123 150 243
Two smallest: 105 + 123 = 228
Sorted list: 150 228 243
Two smallest: 150 + 228 = 378
Sorted list: 243 378
Two smallest: 243 + 378 = 621
Last item is 621.
</pre>
3,026

edits