Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -0,0 +1,58 @@
class
BOGO_SORT
feature
bogo_sort(ar: ARRAY[INTEGER]): ARRAY[INTEGER]
do
from
until
is_sorted (ar) = TRUE
loop
Result:= shuffel(ar)
end
end
feature{NONE}
is_sorted (ar:ARRAY[INTEGER]): BOOLEAN
require
not_void: ar /= Void
local
i: INTEGER
do
Result := True
from
i := 1+ 1
invariant
i >= 1 + 1 and i <= ar.count + 1
until
i > ar.count
loop
Result := Result and ar [i - 1] <= ar [i]
i := i + 1
variant
ar.count + 1 - i
end
end
shuffel(ar:ARRAY[INTEGER]): ARRAY[INTEGER]
require
not_void: ar/= Void
local
i,j:INTEGER
ith: INTEGER
random: V_RANDOM
do
create random
from
i:=ar.count
until
i<2
loop
j:=random.bounded_item (1, i)
ith:= ar[i]
ar[i]:= ar[j]
ar[j]:= ith
random.forth
i:=i-1
end
Result:= ar
end
end

View file

@ -0,0 +1,24 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
do
test:= <<3,2,5,7,1>>
io.put_string ("Unsorted: ")
across test as t loop io.put_string (t.item.out + " ") end
create sorter
test:= sorter.bogo_sort (test)
io.put_string ("%NSorted: ")
across test as t loop io.put_string (t.item.out + " ") end
end
test: ARRAY[INTEGER]
sorter: BOGO_SORT
end