Created
March 23, 2012 13:03
-
-
Save timjb/2170459 to your computer and use it in GitHub Desktop.
Informatik-Unterricht: Zählen der Vergleichsoperationen beim Sortieren eines Arrays
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
| // $ 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