Nodejs Array Quick Sort quickSort()

Here you can find the source of quickSort()

Method Source Code

Array.prototype.quickSort = function () {
  if (this.length < 2) {
    return this;/*w w 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);

Related

  1. quicksort(A, p, q)
    Array.prototype.swap = function(i, j) {
      var tmp = this[i];
      this[i] = this[j];
      this[j] = tmp;
    function partition(A, p, q, r) {
      var pivot = A[r];
      A.swap(r, p);
      var i = p;
    ...
    
  2. quicksort(A, p, r)
    "use strict";
    function main(A) {
        quicksort(A, 0, A.length - 1);
        console.log(A);
    function quicksort(A, p, r) {
        if (p < r) {
            var q = partition(A, p, r);
            quicksort(A, p, q - 1);
    ...
    
  3. quickSort()
    var quick = function(arr) {
      if(arr.length <= 1){
        return arr;
      var pivotIndex = Math.floor(arr.length / 2);
      var pivot = arr.splice(pivotIndex, 1)[0];
      var left = [];
      var right = [];
      var same = [];
    ...
    
  4. 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])
    ...
    
  5. 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++) {
    ...
    
  6. 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++) {
    ...
    
  7. 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 = [];
    ...
    
  8. quickSort(comparator)
    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;
    ...
    
  9. 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) {
    ...