Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,38 +1,63 @@
class
GNOME_SORT
GNOME_SORT [G -> COMPARABLE]
feature
sort(ar: ARRAY[INTEGER]): ARRAY[INTEGER]
-- sort array ar with gnome sort
require
array_not_void: ar/= VOID
local
i,j, ith: INTEGER
do
from
i:= 2
j:= 3
until
i>ar.count
loop
if ar[i-1] <= ar[i] then
i:= j
j:= j+1
else
ith := ar[i-1]
ar[i-1] := ar[i]
ar[i] := ith
i:= i-1
if i=1 then
i:=j
j:= j+1
sort (ar: ARRAY [G]): ARRAY [G]
-- Sorted array in ascending order.
require
array_not_void: ar /= Void
local
i, j: INTEGER
ith: G
do
create Result.make_empty
Result.deep_copy (ar)
from
i := 2
j := 3
until
i > Result.count
loop
if Result [i - 1] <= Result [i] then
i := j
j := j + 1
else
ith := Result [i - 1]
Result [i - 1] := Result [i]
Result [i] := ith
i := i - 1
if i = 1 then
i := j
j := j + 1
end
end
end
end
Result := ar
ensure
same_length: ar.count = Result.count
same_items: Result.same_items (ar)
end
Same_length: ar.count = Result.count
Result_is_sorted: is_sorted (Result)
end
feature {NONE}
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
end

View file

@ -1,21 +1,33 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
make
feature
make
do
test:= <<7, 99, -7, 1, 0, 25, -10>>
create gnome
test:= gnome.sort (test)
across test as ar loop io.put_string( ar.item.out + "%T") end
end
test: ARRAY[INTEGER]
gnome: GNOME_SORT[INTEGER]
make
do
test := <<7, 99, -7, 1, 0, 25, -10>>
io.put_string ("unsorted:%N")
across
test as ar
loop
io.put_string (ar.item.out + "%T")
end
io.new_line
io.put_string ("sorted:%N")
create gnome
test := gnome.sort (test)
across
test as ar
loop
io.put_string (ar.item.out + "%T")
end
end
test: ARRAY [INTEGER]
gnome: GNOME_SORT [INTEGER]
end