Sorting algorithms/Bogosort: Difference between revisions
Content deleted Content added
Line 465: | Line 465: | ||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<lang Eiffel> |
<lang Eiffel> |
||
class |
class |
||
BOGO_SORT |
BOGO_SORT |
||
feature |
feature |
||
bogo_sort(ar: ARRAY[INTEGER]): ARRAY[INTEGER] |
bogo_sort (ar: ARRAY [INTEGER]): ARRAY [INTEGER] |
||
⚫ | |||
-- Sorted array in ascending order. |
|||
⚫ | |||
do |
|||
⚫ | |||
is_sorted (ar) = TRUE |
|||
until |
|||
is_sorted (ar) = TRUE |
|||
loop |
|||
Result := shuffel (ar) |
|||
⚫ | |||
end |
end |
||
⚫ | |||
feature{NONE} |
feature {NONE} |
||
is_sorted (ar:ARRAY[INTEGER]): BOOLEAN |
is_sorted (ar: ARRAY [INTEGER]): BOOLEAN |
||
-- Is 'ar' sorted in ascending order? |
|||
require |
require |
||
not_void: ar /= Void |
not_void: ar /= Void |
||
Line 486: | Line 493: | ||
Result := True |
Result := True |
||
from |
from |
||
i := 1+ 1 |
i := 1 + 1 |
||
invariant |
invariant |
||
i >= 1 + 1 and i <= ar.count + 1 |
i >= 1 + 1 and i <= ar.count + 1 |
||
Line 499: | Line 506: | ||
end |
end |
||
shuffel(ar:ARRAY[INTEGER]): ARRAY[INTEGER] |
shuffel (ar: ARRAY [INTEGER]): ARRAY [INTEGER] |
||
-- Array containing the same elements as 'ar' in a random shuffled order. |
|||
require |
|||
not_void: ar /= Void |
|||
local |
|||
i,j:INTEGER |
|||
i, j: INTEGER |
|||
⚫ | |||
random: V_RANDOM |
|||
do |
|||
⚫ | |||
create random |
|||
from |
|||
create Result.make_empty |
|||
i:=ar.count |
|||
Result.deep_copy (ar) |
|||
⚫ | |||
from |
|||
i := ar.count |
|||
⚫ | |||
⚫ | |||
i < 2 |
|||
ith:= ar[i] |
|||
loop |
|||
ar[i]:= ar[j] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Result [i] := Result [j] |
|||
i:=i-1 |
|||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
i := i - 1 |
|||
end |
|||
end |
|||
end |
end |
||
</lang > |
</lang > |
||
TEST: |
TEST: |
||
<lang Eiffel> |
<lang Eiffel> |
||
class |
class |
||
APPLICATION |
APPLICATION |
||
inherit |
|||
ARGUMENTS |
|||
create |
create |
||
Line 539: | Line 548: | ||
make |
make |
||
do |
do |
||
test:= <<3,2,5,7,1>> |
test := <<3, 2, 5, 7, 1>> |
||
io.put_string ("Unsorted: ") |
io.put_string ("Unsorted: ") |
||
across |
|||
⚫ | |||
test as t |
|||
loop |
|||
⚫ | |||
end |
|||
create sorter |
create sorter |
||
test:= sorter.bogo_sort (test) |
test := sorter.bogo_sort (test) |
||
io.put_string ("%NSorted: ") |
io.put_string ("%NSorted: ") |
||
across |
|||
⚫ | |||
test as t |
|||
loop |
|||
⚫ | |||
end |
|||
end |
end |
||
⚫ | |||
test: ARRAY [INTEGER] |
|||
⚫ | |||
⚫ | |||
end |
end |
||
</lang> |
</lang> |
||
{{out}} |
{{out}} |