Skip to content

Instantly share code, notes, and snippets.

@willwright82
Created July 13, 2017 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willwright82/2fcbd7a6ff22b2d675b04050421501c8 to your computer and use it in GitHub Desktop.
Save willwright82/2fcbd7a6ff22b2d675b04050421501c8 to your computer and use it in GitHub Desktop.
Bogosort algorithm implementation in Javascript
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