Nodejs Array Quick Sort quicksort(A, p, r)

Here you can find the source of quicksort(A, p, r)

Method Source Code

"use strict";//w w  w  .j a  v a2  s  .c o  m

function main(A) {

    quicksort(A, 0, A.length - 1);
    console.log(A);
    
}

function quicksort(A, p, r) {

    if (p < r) {
        // the array is partitioned in two parts, in the left part all the values are smaller than the pivot, in the
        // right part all the values are larger than the pivot
        var q = partition(A, p, r);
        // the same process is applied to the left and right part
        quicksort(A, p, q - 1);
        quicksort(A, q + 1, r);
    }

}

function partition(A, p, r) {

    // we take the last element of the array as pivot
    var x = A[r];
    var i = p - 1;

    // we compare each element with the pivot, if its value is smaller the element is moved in the left part of the
    // array, i is the limit between the left an right part of the array
    for (var j = p; j < r; j++) {
        if (A[j] <= x) {
            i++;
            A.swap(i, j);
        }
    }

    // we move the pivot right after i so that we have an array where the left values are smaller than the pivot and
    // the right values are larger than the pivot
    A.swap(++i, r);

    return i;
}

Array.prototype.swap = function (x,y) {
    var b = this[x];
    this[x] = this[y];
    this[y] = b;
    return this;
}

main([13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11]);

Related

  1. quickSort(A)
    function quickSort(A) {
      if (A.length === 0) {
        return [];
      if (A.length === 1 || (A.length === 2 && A[0] === A[1])) {
        return A;
      let p = A.findBasePoint();
      let leftPart = [],
    ...
    
  2. quickSort(a,start,end)
    'use strict'
    Array.prototype.showArray = function(){
        var temp = '';
        this.forEach(function(value,index){
            temp += value + " ";
        });
        if(typeof document !== "undefined"){
            document.body.innerHTML = temp;
        else{
            console.log(temp);
    };
    function partition(a,start,end){
        var i = start,j = end;
        var key = a[i];
        while(i<j){
            while(i<j && a[j] >= key){
                j--;
            if(i<j){
                var temp;
                temp = a[i];
                a[i] = a[j];
                a[j] = temp; 
            while(i<j && a[i] <= key){
                i++;
            if(i<j){
                var temp;
                temp = a[i];
                a[i] = a[j];
                a[j] = temp; 
        return i;
    function quickSort(a,start,end){
        var middle;
        if(start<end){
            middle = partition(a,start,end);
            quickSort(a,start,middle-1);
            quickSort(a,middle,end - 1);
    var a = [6,2,7,3,8,9];
    quickSort(a,0,5);
    a.showArray();
    
  3. 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;
    ...
    
  4. 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 = [];
    ...
    
  5. 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])
    ...
    
  6. 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++) {
    ...
    
  7. 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) {
    ...