Last active
January 1, 2016 16:39
-
-
Save guanix/8172370 to your computer and use it in GitHub Desktop.
Bogosort and Bogobogosort in Julia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bogosort(v::Array) | |
if length(v) == 1 | |
return v | |
end | |
c = deepcopy(v) | |
while !issorted(c) | |
shuffle!(c) | |
end | |
c | |
end | |
function isbogobogosorted(v::Array) | |
if length(v) == 1 | |
return v | |
end | |
c = deepcopy(v) | |
clen = length(c) | |
a = bogobogosort(c[1:clen-1]) | |
while c[clen] < a[clen-1] | |
shuffle!(c) | |
a = bogobogosort(c[1:clen-1]) | |
end | |
# a is now sorted | |
# check if this copy is in same order as original list | |
c = [a, c[clen]] | |
c == v | |
end | |
function bogobogosort(v::Array) | |
if length(v) == 1 | |
return v | |
end | |
c = deepcopy(v) | |
while !isbogobogosorted(c) | |
shuffle!(c) | |
end | |
c | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment