Nodejs Array Sort Array.prototype.qsort(left, right)

Here you can find the source of Array.prototype.qsort(left, right)

Method Source Code

// From TiCPU, http://forum.espruino.com/conversations/568

// Quick sort//from   w  w w .  j  av a  2  s.com
Array.prototype.qsort = function (left, right) {
  var pivot, newPivot, pivotVal, tmp;
  if (left === undefined) {
    left = 0;
    right = this.length - 1;
  }
  if (left < right) {
    pivot = left + Math.ceil((right - left) / 2);
    // Partition
    pivotVal = this[pivot];
    newPivot = left;
    this[pivot] = this[right]; this[right] = pivotVal;
    for (var i = left; i < right; i++) {
      if (this[i] < pivotVal) {
        tmp = this[i]; this[newPivot++] = i; this[i] = tmp;
      }
    }
    this.qsort(left, newPivot - 1);
    this.qsort(newPivot + 1, right);
  }
};
Uint16Array.prototype.qsort = Array.prototype.qsort;

// Tests
var tQsort = new Uint16Array([5454,5449,5380,5412,5380,5366,5344,5395,5398,5424,5422,5473,5420,5432,5376,5354,5561,5288,5393,5388,5422,5427,5476,5407,5385,5180,5363,5324,5395,5393,5410,5405,5349,5361,5385,5412,5373,5373,5478,5420,5446,5395,5339,5407,5420,5356,5336,5427,5459,5378,5336,5349,5420,5405,5434,5383,5446,5422,5349,5329,5405,5434,5446,5336,5427,5473,5402,5170,5388,5412,5456,123,456,789]);
tQsort.qsort();

Related

  1. sleepSort()
    Array.prototype.sleepSort = function () {
      var _arr = [];
      this.forEach(function (number) {
        setTimeout(function () {
          _arr.push(number);
        }, 2 * number);
      });
      return _arr;
    
  2. stableSort(f)
    Array.prototype.stableSort = function(f) {
      return this.map((ele, i) => ({ele: ele, i: i}))
      .sort((a, b) => {
        var val = f(a.ele, b.ele);
        if (val === 0) {
          return a.i - b.i;
        return val;
      }).map(ele => {
    ...
    
  3. superSort()
    Array.prototype.superSort = function() {
        function dynamicSort(property) {
            return function (obj1,obj2) {
                return obj1[property] > obj2[property] ? 1
                    : obj1[property] < obj2[property] ? -1 : 0;
        var props = arguments;
        return this.sort(function (obj1, obj2) {
    ...
    
  4. timeoutSort(f)
    Array.prototype.timeoutSort = function (f) {
      this.forEach(function (n) {
        setTimeout(function () { f(n) }, 5 * n)
      });
    
  5. validate_sorted()
    Array.prototype.validate_sorted = function(){
      for (var i = 0; i < this.length - 1; i++){
        if (this[i] > this[i+1])
          return false;
      return true;
    };