Sorting algorithms/Cocktail sort: Difference between revisions

Added an Algol W sample
(Added an Algol W sample)
Line 166:
break do od loop: SKIP
);</lang>
 
=={{header|ALGOL W}}==
As noted in the ALGOL 68 sample above, the highest and lowest elements are sorted into their correct positions each time through the main loop.
This implementation optimises by reducing the number of elements to sort on each pass through the main loop.
<lang algolw>begin
% As algol W does not allow overloading, we have to have type-specific %
% sorting procedures - this coctail sorts an integer array %
% as there is no way for the procedure to determine the array bounds, we %
% pass the lower and upper bounds in lb and ub %
procedure coctailSortIntegers( integer array item( * )
; integer value lb
; integer value ub
) ;
begin
integer lower, upper;
 
lower := lb;
upper := ub - 1;
 
while
begin
logical swapped;
 
procedure swap( integer value i ) ;
begin
integer val;
val := item( i );
item( i ) := item( i + 1 );
item( i + 1 ) := val;
swapped := true;
end swap ;
 
swapped := false;
for i := lower until upper do if item( i ) > item( i + 1 ) then swap( i );
if swapped
then begin
% there was at least one unordered element so try a 2nd sort pass %
for i := upper step -1 until lower do if item( i ) > item( i + 1 ) then swap( i );
upper := upper - 1; lower := lower + 1;
end if_swapped ;
swapped
end
do begin end;
end coctailSortIntegers ;
 
begin % test the sort %
integer array data( 1 :: 10 );
 
procedure writeData ;
begin
write( data( 1 ) );
for i := 2 until 10 do writeon( data( i ) );
end writeData ;
 
% initialise data to unsorted values %
integer dPos;
dPos := 1;
for i := 16, 2, -6, 9, 90, 14, 0, 23, 8, 9
do begin
data( dPos ) := i;
dPos := dPos + 1;
end for_i ;
 
i_w := 3; s_w := 1; % set output format %
writeData;
coctailSortIntegers( data, 1, 10 );
writeData;
end test ;
end.</lang>
{{out}}
<pre>
16 2 -6 9 90 14 0 23 8 9
-6 0 2 8 9 9 14 16 23 90
</pre>
 
=={{header|AutoHotkey}}==
3,044

edits