Skip to content

Instantly share code, notes, and snippets.

@A
Last active August 29, 2015 14:00
Show Gist options
  • Save A/11259133 to your computer and use it in GitHub Desktop.
Save A/11259133 to your computer and use it in GitHub Desktop.
bogosort.js
var isSorted = function (arr) {
for (var i in arr) {
if (i === 1) next;
if (arr[i-1] > arr[i]) return false;
}
return true;
};
var shuffle = function (arr) {
return arr.sort(function() {
return .5 - Math.random();
});
};
var bogosort = function (arr) {
var i = 0;
while (!isSorted(arr)) {
shuffle(arr);
i++;
}
};
bogosort([2,1,3,4,6,5,7,8,9,10])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment