Skip to content

Instantly share code, notes, and snippets.

@callumacrae
Created February 11, 2012 23:06
Show Gist options
  • Select an option

  • Save callumacrae/1804907 to your computer and use it in GitHub Desktop.

Select an option

Save callumacrae/1804907 to your computer and use it in GitHub Desktop.
Bogosort
Array.prototype.isSorted = function () {
for (var i = 1; i < this.length; i++) {
if (this[i] < this[i - 1]) {
return false;
}
}
return true;
};
Array.prototype.shuffle = function () {
var ary = [], clone = this.slice();
while (clone.length) {
ary = ary.concat(clone.splice(Math.floor(Math.random() * clone.length), 1));
}
return ary;
};
Array.prototype.bogosort = function () {
var ary = this.shuffle();
while (!ary.isSorted()) {
ary = ary.shuffle();
}
return ary;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment