Javascript Array quickSort() method

Description

Javascript Array quickSort() method


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])//ww  w  .  j ava2  s .  co m
      } else {
        right.push(this[i])
      }
    }
    return left.quickSort().concat(pivot, right.quickSort())
  } else {
    return this
  }
}

Javascript Array quicksort()

// Quicksort algorithm implementation
Array.prototype.quicksort = function (){
 var a_left = [];
    var a_right = [];
 if (this.length <= 1) return this;
 for (var i = 1; i < this.length; i++) {
  if (this[i]<this[0]) {
   a_left.push(this[i]);/*w w  w . j av  a2s  .  com*/
  } else if (this[i]>this[0]) {
   a_right.push(this[i]);
  } else {
   a_right = [this[0]].concat(a_right);
  }
 }
 return a_left.quicksort().concat( this[0] , a_right.quicksort());
}

Javascript Array quickSort()

// Implement Quick Sort

Array.prototype.quickSort = function()
{
  if (this.length === 0) {
    return [];//from   w  ww .  j av a  2 s .c o  m
  }

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

  for (var i = 1; i < this.length; i++) {
      if (this[i] < pivot) {
         left.push(this[i]);
      } else {
         right.push(this[i]);
      }
  }
  return left.quickSort().concat(pivot, right.quickSort());
}

console.log();
console.log( JSON.stringify( [].quickSort() ) == JSON.stringify( [] ) );
console.log( JSON.stringify( [3,7,8,5,2,1,9,5,4].quickSort() ) == JSON.stringify( [1,2,3,4,5,5,7,8,9] ) );
console.log();

Javascript Array quickSort()

//Hint: quicksort checks by pivoting!


Array.prototype.quickSort = function () {
  if (this.length < 2) {
    return this;/*from   ww  w. ja  va 2 s. c  o m*/
  }
  let pivot = this[0];
  let left = [];
  let right = [];
  for (var i = 1; i < this.length; i++) {
    if (this[i] < pivot) {
      left.push(this[i]);
    } else {
      right.push(this[i]);
    }
  }

  return left.quickSort().concat(pivot, right.quickSort());
};

// this call back is essentially the prc we would pass in ruby.


const test = [7,3,5,2,8,7,1,9,3,4,].quickSort();
console.log(test);

Javascript Array quickSort()

Array.prototype.quickSort = function () {
    if (this.length <= 1) {
        return this;
    }// w  ww.  j  a  v  a 2s  . c om
    var pointIndex = Math.floor(this.length / 2);
    var pointValue = this.splice(pointIndex, 1);
    var centerValue = pointValue[0];
    var left = [];
    var right = [];

    for (var i = 0; i < this.length; i++) {
        var cur = this[i];
        cur < centerValue ? left.push(cur) : right.push(cur);

    }

    return left.quickSort().concat(pointValue, right.quickSort());

}
var ary = [3, 2, 1, 10, 7, 8, 9];
console.log(ary.quickSort());

Javascript Array quicksort()

Array.prototype.quicksort = function (){
    if (this.length === 0) return [];
    // Select a pivot point (I chose the last element, but you can sample any number)
    var pivot = this[0];
    var left = [],
        right = [];/*  w w  w  .j a  v  a 2s  .c om*/

    // Iterate through array, putting numbers smaller than pivot in one array,
    // and larger numbers in the other
    for (var i = 1; i < this.length; i++) {
          if (this[i] < pivot) {
             left.push(this[i]);
          } else {
             right.push(this[i]);
          }
    }

    // Call quick sort recurisvely on the two subarrays, and combine the results
    // together with the pivot point
    return left.quicksort().concat([pivot], right.quicksort());
};



PreviousNext

Related