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,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

View file

@ -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

View file

@ -0,0 +1,13 @@
defmodule Sort do
def bogo_sort(list) do
if sorted?(list) do
list
else
bogo_sort(Enum.shuffle(list))
end
end
defp sorted?(list) when length(list)<=1, do: true
defp sorted?([x, y | _]) when x>y, do: false
defp sorted?([_, y | rest]), do: sorted?([y | rest])
end

View file

@ -0,0 +1,25 @@
function isordered{T}(a::AbstractArray{T,1})
if length(a) < 2
return true
end
for i in 2:length(a)
if a[i] < a[i-1]
return false
end
end
return true
end
function bogosort!{T}(a::AbstractArray{T,1})
while !isordered(a)
shuffle!(a)
end
return a
end
a = [rand(-10:10) for i in 1:10]
println("Before bogosort:")
println(a)
bogosort!(a)
println("\nAfter bogosort:")
println(a)

View file

@ -1,2 +1,2 @@
def isSorted(l: List[Int]) = l.iterator sliding 2 forall (s => s.head < s.last)
def isSorted(l: List[Int]) = l.iterator sliding 2 forall (s => s.head <= s.last)
def bogosort(l: List[Int]): List[Int] = if (isSorted(l)) l else bogosort(scala.util.Random.shuffle(l))