Skip to content

Instantly share code, notes, and snippets.

@timjb
Created March 23, 2012 13:03
Show Gist options
  • Select an option

  • Save timjb/2170459 to your computer and use it in GitHub Desktop.

Select an option

Save timjb/2170459 to your computer and use it in GitHub Desktop.
Informatik-Unterricht: Zählen der Vergleichsoperationen beim Sortieren eines Arrays
// $ node count-compare-operations.js > abc.txt
// $ R
// > plot(read.table("abc.txt"))
function generate_ints(n) {
var arr = [];
while (n--) {
arr.push(Math.floor(Math.random()*1000));
}
return arr;
}
function main () {
var n = 10;
while (n < 500000) {
n = Math.floor(n*1.5);
var arr = generate_ints(n);
var cmpOperations = 0;
arr.sort(function (a, b) {
cmpOperations++;
if (a < b) return -1;
if (a === b) return 0;
return 1;
});
console.log(n + ' ' + cmpOperations);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment