Created
May 20, 2014 04:32
-
-
Save jamesls/4cec26e9ba83ff0601a1 to your computer and use it in GitHub Desktop.
Javascript sort stability
This file contains 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
function sortArray(n) { | |
var array = []; | |
for (var i = 1; i <= n; i++) { | |
array.push({key: 10, name: i.toString()}); | |
} | |
array.sort(function(a, b) { | |
return a.key - b.key; | |
}); | |
for(var j = 0; j < n; j++) { | |
process.stdout.write(array[j].name + " "); | |
} | |
} | |
for (var i = 5; i <= 15; i++) { | |
sortArray(i); | |
process.stdout.write(" (Array size: " + i + ")\n"); | |
} | |
/* Output from running: | |
1 2 3 4 5 (Array size: 5) | |
1 2 3 4 5 6 (Array size: 6) | |
1 2 3 4 5 6 7 (Array size: 7) | |
1 2 3 4 5 6 7 8 (Array size: 8) | |
1 2 3 4 5 6 7 8 9 (Array size: 9) | |
1 2 3 4 5 6 7 8 9 10 (Array size: 10) | |
6 1 3 4 5 2 7 8 9 10 11 (Array size: 11) | |
7 1 3 4 5 6 2 8 9 10 11 12 (Array size: 12) | |
7 1 3 4 5 6 2 8 9 10 11 12 13 (Array size: 13) | |
8 1 3 4 5 6 7 2 9 10 11 12 13 14 (Array size: 14) | |
8 1 3 4 5 6 7 2 9 10 11 12 13 14 15 (Array size: 15) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment