Skip to content

Instantly share code, notes, and snippets.

@guanix
Last active January 1, 2016 16:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guanix/8172370 to your computer and use it in GitHub Desktop.
Save guanix/8172370 to your computer and use it in GitHub Desktop.
Bogosort and Bogobogosort in Julia
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