Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,65 @@
|
|||
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.
|
||||
|
|
@ -1,50 +1,90 @@
|
|||
class
|
||||
COCKTAIL_SORT[G -> COMPARABLE]
|
||||
COCKTAIL_SORT [G -> COMPARABLE]
|
||||
|
||||
feature
|
||||
cocktail_sort(ar: ARRAY[G]): ARRAY[G]
|
||||
require
|
||||
ar_not_empty: ar.count>=1
|
||||
local
|
||||
swapped, finished: BOOLEAN
|
||||
sol: ARRAY[G]
|
||||
i,j : INTEGER
|
||||
t: G
|
||||
do
|
||||
create sol.make_from_array (ar)
|
||||
from
|
||||
until finished= TRUE
|
||||
loop
|
||||
swapped := FALSE
|
||||
from
|
||||
i:= 1
|
||||
until
|
||||
i= ar.count-1
|
||||
loop
|
||||
if ar[i]> ar[i+1] then
|
||||
t:= ar[i]
|
||||
ar[i]:= ar[i+1]
|
||||
ar[i+1]:= t
|
||||
swapped:= true
|
||||
|
||||
cocktail_sort (ar: ARRAY [G]): ARRAY [G]
|
||||
-- Array sorted in ascending order.
|
||||
require
|
||||
ar_not_empty: ar.count >= 1
|
||||
local
|
||||
not_swapped: BOOLEAN
|
||||
sol: ARRAY [G]
|
||||
i, j: INTEGER
|
||||
t: G
|
||||
do
|
||||
create Result.make_empty
|
||||
Result.deep_copy (ar)
|
||||
from
|
||||
until
|
||||
not_swapped = True
|
||||
loop
|
||||
not_swapped := True
|
||||
from
|
||||
i := Result.lower
|
||||
until
|
||||
i = Result.upper - 1
|
||||
loop
|
||||
if Result [i] > Result [i + 1] then
|
||||
Result := swap (Result, i)
|
||||
not_swapped := False
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
from
|
||||
j := Result.upper - 1
|
||||
until
|
||||
j = Result.lower
|
||||
loop
|
||||
if Result [j] > Result [j + 1] then
|
||||
Result := swap (Result, j)
|
||||
not_swapped := False
|
||||
end
|
||||
j := j - 1
|
||||
end
|
||||
end
|
||||
i:= i+1
|
||||
ensure
|
||||
ar_is_sorted: is_sorted (Result)
|
||||
end
|
||||
|
||||
from j:= ar.count-1
|
||||
until j= 1
|
||||
loop
|
||||
if ar[j]> ar[j+1] then
|
||||
t:= ar[j]
|
||||
ar[j]:= ar[j+1]
|
||||
ar[j+1]:= t
|
||||
swapped:= TRUE
|
||||
feature{NONE}
|
||||
|
||||
swap (ar: ARRAY [G]; i: INTEGER): ARRAY [G]
|
||||
-- Array with elements i and i+1 swapped.
|
||||
require
|
||||
ar_not_void: ar /= Void
|
||||
i_is_in_bounds: ar.valid_index (i)
|
||||
local
|
||||
t: G
|
||||
do
|
||||
create Result.make_empty
|
||||
Result.deep_copy (ar)
|
||||
t := Result [i]
|
||||
Result [i] := Result [i + 1]
|
||||
Result [i + 1] := t
|
||||
ensure
|
||||
swapped_right: Result [i + 1] = ar [i]
|
||||
swapped_left: Result [i] = ar [i + 1]
|
||||
end
|
||||
j:= j-1
|
||||
|
||||
is_sorted (ar: ARRAY [G]): BOOLEAN
|
||||
--- Is 'ar' sorted in ascending order?
|
||||
require
|
||||
ar_not_empty: ar.is_empty = False
|
||||
local
|
||||
i: INTEGER
|
||||
do
|
||||
Result := True
|
||||
from
|
||||
i := ar.lower
|
||||
until
|
||||
i = ar.upper
|
||||
loop
|
||||
if ar [i] > ar [i + 1] then
|
||||
Result := False
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
end
|
||||
if swapped= FALSE then
|
||||
finished:= TRUE
|
||||
sol:= ar
|
||||
end
|
||||
end
|
||||
Result:= sol
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,18 +1,33 @@
|
|||
class
|
||||
APPLICATION
|
||||
inherit
|
||||
ARGUMENTS
|
||||
|
||||
create
|
||||
make
|
||||
feature
|
||||
make
|
||||
do
|
||||
test:= <<5,1,99,3,2>>
|
||||
across test as t loop io.put_string (t.item.out + "%T") end
|
||||
create cs
|
||||
test:= cs.cocktail_sort(test)
|
||||
across test as ar loop io.put_string (ar.item.out+"%T") end
|
||||
end
|
||||
cs: COCKTAIL_SORT[INTEGER]
|
||||
test: ARRAY[INTEGER]
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
do
|
||||
test := <<5, 1, 99, 3, 2>>
|
||||
io.put_string ("unsorted%N")
|
||||
across
|
||||
test as t
|
||||
loop
|
||||
io.put_string (t.item.out + "%T")
|
||||
end
|
||||
io.new_line
|
||||
io.put_string ("sorted%N")
|
||||
create cs
|
||||
test := cs.cocktail_sort (test)
|
||||
across
|
||||
test as ar
|
||||
loop
|
||||
io.put_string (ar.item.out + "%T")
|
||||
end
|
||||
end
|
||||
|
||||
cs: COCKTAIL_SORT [INTEGER]
|
||||
|
||||
test: ARRAY [INTEGER]
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
defmodule Sort do
|
||||
def cocktail_sort(list) when is_list(list), do: cocktail_sort(list, [], [])
|
||||
|
||||
defp cocktail_sort([], minlist, maxlist), do: Enum.reverse(minlist, maxlist)
|
||||
defp cocktail_sort([x], minlist, maxlist), do: Enum.reverse(minlist, [x | maxlist])
|
||||
defp cocktail_sort(list, minlist, maxlist) do
|
||||
{max, rev} = cocktail_max(list, [])
|
||||
{min, rest} = cocktail_min(rev, [])
|
||||
cocktail_sort(rest, [min | minlist], [max | maxlist])
|
||||
end
|
||||
|
||||
defp cocktail_max([max], list), do: {max, list}
|
||||
defp cocktail_max([x,y | t], list) when x<y, do: cocktail_max([y | t], [x | list])
|
||||
defp cocktail_max([x,y | t], list) , do: cocktail_max([x | t], [y | list])
|
||||
|
||||
defp cocktail_min([min], list), do: {min, list}
|
||||
defp cocktail_min([x,y | t], list) when x>y, do: cocktail_min([y | t], [x | list])
|
||||
defp cocktail_min([x,y | t], list) , do: cocktail_min([x | t], [y | list])
|
||||
end
|
||||
|
||||
IO.inspect Sort.cocktail_sort([5,3,9,4,1,6,8,2,7])
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
function coctailsort{T<:Real}(a::Array{T,1})
|
||||
b = copy(a)
|
||||
isordered = false
|
||||
lo = one(Int)
|
||||
hi = length(b)
|
||||
while !isordered && hi > lo
|
||||
isordered = true
|
||||
for i in lo+1:hi
|
||||
if b[i] < b[i-1]
|
||||
t = b[i]
|
||||
b[i] = b[i-1]
|
||||
b[i-1] = t
|
||||
isordered = false
|
||||
end
|
||||
end
|
||||
hi -= 1
|
||||
if isordered || hi <= lo
|
||||
break
|
||||
end
|
||||
for i in hi:-1:lo+1
|
||||
if b[i-1] > b[i]
|
||||
t = b[i]
|
||||
b[i] = b[i-1]
|
||||
b[i-1] = t
|
||||
isordered = false
|
||||
end
|
||||
end
|
||||
lo += 1
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
a = [rand(-2^10:2^10) for i in 1:20]
|
||||
println("Before Sort:")
|
||||
println(a)
|
||||
a = coctailsort(a)
|
||||
println("\nAfter Sort:")
|
||||
println(a)
|
||||
Loading…
Add table
Add a link
Reference in a new issue