Nodejs Array Quick Sort quickSort(comparator)

Here you can find the source of quickSort(comparator)

Method Source Code

// Quicksort/*from w  w  w . ja  v  a 2  s  .  c o m*/
Array.prototype.quickSort = function (comparator) {
  if (typeof comparator !== "function") {
    comparator = function (x, y) {
      if (x === y) {
        return 0;
      } else if (x < y) {
        return -1;
      } else {
        return 1;
      }
    };
  }

  if (this.length < 2) {
    return this;
  }

  const pivot = this[0];
  const left = [];
  const right = [];

  for (let i = 1; i < this.length; i++) {
    if (comparator == undefined) {
      if (this[i] < pivot) {
        left.push(this[i]);
      } else {
        right.push(this[i]);
      }
    } else {
      if (comparator(this[i], pivot) < 0) {
        left.push(this[i]);
      } else {
        right.push(this[i]);
      }
    }
  }

  const lsort = left.quickSort(comparator);
  lsort.push(pivot);
  const rsort = right.quickSort(comparator);
  const sorted = lsort.concat(rsort);
  return sorted;
};

Related

  1. quickSort()
    Array.prototype.quickSort = function() {
      var len = this.length
      if(len > 1) {
        var pivot = this[0]
        var left = []
        var right = []
        for(var i = 1; i < len; i++) {
          if(this[i] < pivot) {
            left.push(this[i])
    ...
    
  2. quickSort()
    Array.prototype.quickSort = function() {
      var len = this.length;
      if (len <= 1) {
        return this.slice(0);
      var left = [];
      var right = [];
      var mid = [this[0]];
      for (var i = 1; i < len; i++) {
    ...
    
  3. quickSort()
    Array.prototype.quickSort = function () {
      if (this.length < 2) {
        return this;
      let pivot = this[0];
      let left = [];
      let right = [];
      for (var i = 1; i < this.length; i++) {
        if (this[i] < pivot) {
    ...
    
  4. quickSort()
    Array.prototype.quickSort = function()
      if (this.length === 0) {
        return [];
      var left = [];
      var right = [];
      var pivot = this[0];
      for (var i = 1; i < this.length; i++) {
    ...
    
  5. quickSort()
    Array.prototype.quickSort = function () {
        if (this.length <= 1) {
            return this;
        var pointIndex = Math.floor(this.length / 2);
        var pointValue = this.splice(pointIndex, 1);
        var centerValue = pointValue[0];
        var left = [];
        var right = [];
    ...
    
  6. quickSort(start, len)
    Array.prototype.quickSort = function(start, len) {
      if (typeof len === "undefined") {
        len = this.length;
      if (len < 2) {
        return this;
    };
    Array.prototype.swap = function (idx1, idx2) {
    ...
    
  7. quick_sort()
    Array.prototype.quick_sort = function () {
        if (this.length < 2) { return this; }
        var pivot = this[Math.round(this.length / 2)];
        return this.filter(x => x <  pivot)
                   .quick_sort()
                   .concat(this.filter(x => x == pivot))
                   .concat(this.filter(x => x >  pivot).quick_sort());
    };
    
  8. quick_sort()
    Array.prototype.quick_sort = function () {
        if (this.length < 2) { return this; }
        var pivot = this[Math.round(this.length / 2)];
        return this.filter(x => x < pivot)
                   .quick_sort()
                   .concat(this.filter(x => x === pivot))
                   .concat(this.filter(x => x > pivot).quick_sort());
    };
    
  9. quick_sort()
    Array.prototype.quick_sort = function ()
        if (this.length <= 1)
            return this;
        var pivot = this[Math.round(this.length / 2)];
        return this.filter(function (x) { return x <  pivot }).quick_sort().concat(
               this.filter(function (x) { return x == pivot })).concat(
               this.filter(function (x) { return x >  pivot }).quick_sort());