Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function log(text) {
- var div = document.createElement("div");
- div.textContent = text;
- document.body.appendChild(div);
- };
- function intList(n) {
- var i, result = [];
- for(i = 0; i < n; i++) result.push(i);
- return result;
- };
- function time(f) {
- var r, startTime, endTime;
- startTime = Date.now();
- r = f();
- endTime = Date.now();
- log("time = " + (endTime - startTime) + "ms");
- return r;
- };
- function randInt(a, b) {
- return a+Math.floor(Math.random()*(b-a));
- };
- function shuffle(a) {
- var i, idx, temp;
- for(i = 0; i < a.length-1; i++) {
- idx = randInt(i, a.length);
- temp = a[i]; a[i] = a[idx]; a[idx] = temp;
- }
- };
- time(function() {
- nums = intList(1000000);
- });
- time(function() {
- shuffle(nums);
- });
- for(var i = 0; i < 10; i++) log(nums[i]);
- time(function() {
- nums.sort(function(a, b) {return a-b});
- });
- for(var i = 0; i < 10; i++) log(nums[i]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement