Sort

Benchmark created by iKBAHT on


Setup

function qSort(arr, sortFunction) {
    if (sortFunction == undefined) {
      sortFunction = function(a, b) {
        return ((a > b) ? 1 : ((a === b) ? 0 : -1));
      };
    }
  
    sort(0, arr.length - 1);
  
    function sort(l, r) {
      var tmp, s, x,
        ltmp = l,
        rtmp = r;
  
      s = Math.floor((r + l) / 2);
      x = arr[s];
  
      do {
        while (sortFunction(arr[l], x) < 0) l++;
        while (sortFunction(x, arr[r]) < 0) r--;
  
        if (l <= r) {
          if (l < r) {
            tmp = arr[l];
            arr[l] = arr[r];
            arr[r] = tmp;
          }
          l++;
          r--;
        }
      } while (l <= r)
  
      if (ltmp < r) sort(ltmp, r);
      if (l < rtmp) sort(l, rtmp);
    }
  };
  
  function sortFun(a, b) {
    return a - b;
  };
  
  function generateArray(length, maxNumber) {
    var arr = [];
    while (length--) {
      arr.push(Math.random() * maxNumber);
    }
    return arr;
  }
  
  function copy(arr) {
    var newArr = [];
    for (var length = arr.length, i = 0; i < length; ++i) {
      newArr[i] = arr[i];
    }
    return newArr;
  }
  
  var bigArray = generateArray(100000, 1000),
    arr1 = copy(bigArray),
    arr2 = copy(bigArray);

Test runner

Ready to run.

Testing in
TestOps/sec
Native sort
arr1.sort(sortFun);
ready
qSort
qSort(arr2, sortFun);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by iKBAHT on
  • Revision 6: published by iKBAHT on