Created
July 13, 2017 16:48
-
-
Save willwright82/2fcbd7a6ff22b2d675b04050421501c8 to your computer and use it in GitHub Desktop.
Bogosort algorithm implementation in Javascript
This file contains 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
shuffle = function(v) { | |
for (var j, x, i = v.length; i; j = Math.floor(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); | |
return v; | |
}; | |
isSorted = function(v) { | |
for (var i = 1; i < v.length; i++) { | |
if (v[i - 1] > v[i]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
bogosort = function(v) { | |
var sorted = false; | |
while (sorted == false) { | |
v = shuffle(v); | |
sorted = isSorted(v); | |
} | |
//return v; | |
console.log(v); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment