Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,17 +1,23 @@
|
|||
class
|
||||
BOGO_SORT
|
||||
|
||||
feature
|
||||
bogo_sort(ar: ARRAY[INTEGER]): ARRAY[INTEGER]
|
||||
do
|
||||
from
|
||||
until
|
||||
is_sorted (ar) = TRUE
|
||||
loop
|
||||
Result:= shuffel(ar)
|
||||
|
||||
bogo_sort (ar: ARRAY [INTEGER]): ARRAY [INTEGER]
|
||||
-- Sorted array in ascending order.
|
||||
do
|
||||
from
|
||||
until
|
||||
is_sorted (ar) = True
|
||||
loop
|
||||
Result := shuffel (ar)
|
||||
end
|
||||
end
|
||||
end
|
||||
feature{NONE}
|
||||
is_sorted (ar:ARRAY[INTEGER]): BOOLEAN
|
||||
|
||||
feature {NONE}
|
||||
|
||||
is_sorted (ar: ARRAY [INTEGER]): BOOLEAN
|
||||
-- Is 'ar' sorted in ascending order?
|
||||
require
|
||||
not_void: ar /= Void
|
||||
local
|
||||
|
|
@ -19,7 +25,7 @@ feature{NONE}
|
|||
do
|
||||
Result := True
|
||||
from
|
||||
i := 1+ 1
|
||||
i := 1 + 1
|
||||
invariant
|
||||
i >= 1 + 1 and i <= ar.count + 1
|
||||
until
|
||||
|
|
@ -32,27 +38,29 @@ feature{NONE}
|
|||
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
|
||||
shuffle (ar: ARRAY [INTEGER]): ARRAY [INTEGER]
|
||||
-- Array containing the same elements as 'ar' in a shuffled order.
|
||||
require
|
||||
more_than_one_element: ar.count > 1
|
||||
local
|
||||
count, j, ith: INTEGER
|
||||
random: V_RANDOM
|
||||
do
|
||||
create random
|
||||
create Result.make_empty
|
||||
Result.deep_copy (ar)
|
||||
count := ar.count
|
||||
across
|
||||
1 |..| count as c
|
||||
loop
|
||||
j := random.bounded_item (c.item, count)
|
||||
ith := Result [c.item]
|
||||
Result [c.item] := Result [j]
|
||||
Result [j] := ith
|
||||
random.forth
|
||||
end
|
||||
ensure
|
||||
same_elements: across ar as a all Result.has (a.item) end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,24 +1,32 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
inherit
|
||||
ARGUMENTS
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
feature {NONE}
|
||||
|
||||
make
|
||||
do
|
||||
test:= <<3,2,5,7,1>>
|
||||
test := <<3, 2, 5, 7, 1>>
|
||||
io.put_string ("Unsorted: ")
|
||||
across test as t loop io.put_string (t.item.out + " ") end
|
||||
across
|
||||
test as t
|
||||
loop
|
||||
io.put_string (t.item.out + " ")
|
||||
end
|
||||
create sorter
|
||||
test:= sorter.bogo_sort (test)
|
||||
test := sorter.bogo_sort (test)
|
||||
io.put_string ("%NSorted: ")
|
||||
across test as t loop io.put_string (t.item.out + " ") end
|
||||
across
|
||||
test as t
|
||||
loop
|
||||
io.put_string (t.item.out + " ")
|
||||
end
|
||||
end
|
||||
test: ARRAY[INTEGER]
|
||||
sorter: BOGO_SORT
|
||||
|
||||
test: ARRAY [INTEGER]
|
||||
|
||||
sorter: BOGO_SORT
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue