Skip to content

Instantly share code, notes, and snippets.

@benkaiser
Created September 1, 2014 11:38
Show Gist options
  • Select an option

  • Save benkaiser/3b2335f363018ddf824d to your computer and use it in GitHub Desktop.

Select an option

Save benkaiser/3b2335f363018ddf824d to your computer and use it in GitHub Desktop.
Funny sorting function I jokingly came up with last semester that sorts lists with order O(n^n^n).Seriously this is a joke, don't ever use it.
// sort the list in order O(n^n^n)
function jumblesort(list){
while(!sorted(list)){
list = shuffle(list);
}
return list;
}
function shuffle(list) {
for(var i = list.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = list[i];
list[i] = list[j];
list[j] = temp;
}
return list;
}
function sorted(list){
for(var i = 1; i < list.length; i++){
if(list[i] < list[i - 1]){
return false;
}
}
return true;
}
// test it
var list = [];
for(var i = 20; i >= 0; i--){
list.push(i);
}
console.log(jumblesort(list));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment