Created
September 1, 2014 11:38
-
-
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.
This file contains hidden or 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
| // 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