June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,25 @@
|
|||
import extensions.
|
||||
import system'routines.
|
||||
|
||||
extension $op
|
||||
{
|
||||
bogoSorter
|
||||
[
|
||||
var list := self.
|
||||
|
||||
until (list isAscendant)
|
||||
[
|
||||
list := list randomize(list length).
|
||||
].
|
||||
|
||||
^ list
|
||||
]
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var list := (3, 4, 1, 8, 7, -2, 0).
|
||||
|
||||
console printLine("before:", list).
|
||||
console printLine("after :", list bogoSorter).
|
||||
].
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
Public Sub Main()
|
||||
Dim sSorted As String = "123456789" 'The desired outcome
|
||||
Dim sTest, sChr As String 'Various strings
|
||||
Dim iCounter As Integer 'Loop counter
|
||||
|
||||
Do
|
||||
Inc iCounter 'Increase counter value
|
||||
Repeat 'Repeat
|
||||
sChr = Chr(Rand(49, 57)) 'Get a random number and convert it to a character e.g. 49="1"
|
||||
If Not InStr(sTest, sChr) Then sTest &= sChr 'If the random character is not in sTest then add it
|
||||
Until Len(sTest) = 9 'Loop until sTest has 9 characters
|
||||
Print sTest 'Print the string to test
|
||||
If sTest = sSorted Then Break 'If sTest = sSorted then get out of the loop
|
||||
sTest = "" 'Empty sTest and try again
|
||||
Loop
|
||||
|
||||
Print "Solved in " & Str(iCounter) & " loops" 'Print the result
|
||||
|
||||
End
|
||||
|
|
@ -1,25 +1,9 @@
|
|||
function isordered{T}(a::AbstractArray{T,1})
|
||||
if length(a) < 2
|
||||
return true
|
||||
function bogosort!(arr::AbstractVector)
|
||||
while !issorted(arr)
|
||||
shuffle!(arr)
|
||||
end
|
||||
for i in 2:length(a)
|
||||
if a[i] < a[i-1]
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
return arr
|
||||
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)
|
||||
v = rand(-10:10, 10)
|
||||
println("# unordered: $v\n -> ordered: ", bogosort!(v))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
arr := Array([2,3,1]):
|
||||
len := numelems(arr):
|
||||
#Translation of C, random swapping
|
||||
shuffle_arr := proc(arr, len)
|
||||
local i, r, temp:
|
||||
for i from 1 to len do
|
||||
temp := arr[i]:
|
||||
r := rand(1..len)():
|
||||
arr[i] := arr[r]:
|
||||
arr[r] := temp:
|
||||
end do:
|
||||
end proc:
|
||||
while(not ListTools:-Sorted(convert(arr, list))) do
|
||||
shuffle_arr(arr, len):
|
||||
end do:
|
||||
arr;
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
# Project : Sorting algorithms/Bogosort
|
||||
# Date : 2018/02/17
|
||||
# Author : Gal Zsolt (~ CalmoSoft ~)
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
test = [4, 65, 2, 31, 0, 99, 2, 83, 782, 1]
|
||||
shuffles = 0
|
||||
while ! sorted(test)
|
||||
shuffles = shuffles + 1
|
||||
shuffle(test)
|
||||
end
|
||||
see "" + shuffles + " shuffles required to sort " + len(test) + " items:" + nl
|
||||
showarray(test)
|
||||
|
||||
func shuffle(d)
|
||||
for i = len(d) to 2 step -1
|
||||
item = random(i) + 1
|
||||
if item <= len(d)
|
||||
temp = d[i-1]
|
||||
d[i-1] = d[item]
|
||||
d[item] = temp
|
||||
else
|
||||
i = i -1
|
||||
ok
|
||||
next
|
||||
|
||||
func sorted(d)
|
||||
for j = 2 to len(d)
|
||||
if d[j] < d[j-1]
|
||||
return false
|
||||
ok
|
||||
next
|
||||
return true
|
||||
|
||||
func showarray(vect)
|
||||
see "["
|
||||
svect = ""
|
||||
for n = 1 to len(vect)
|
||||
svect = svect + vect[n] + ", "
|
||||
next
|
||||
svect = left(svect, len(svect) - 2)
|
||||
see svect
|
||||
see "]" + nl
|
||||
Loading…
Add table
Add a link
Reference in a new issue