Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue