Nodejs Array Swap swap(idx1, idx2)

Here you can find the source of swap(idx1, idx2)

Method Source Code

function quickSort(arr){

    function partition(low, high){
        if(high - low < 2){
            return
        }//  www.ja  v a2s.c o m
        var pivot = arr[low]
        var wall = low
        for(var i = low; i <= high; i++){
            if(arr[i] < pivot){
                wall += 1
                arr.swap(i, wall)
            }
        }
        arr.swap(low, wall)

        partition(low, wall-1)
        partition(wall+1, high)
    }

    return partition(0, arr.length-1)
}

Array.prototype.swap = function(idx1, idx2){
    var temp = this[idx1]
    this[idx1] = this[idx2]
    this[idx2] = temp
}

var arr = [4, 2, 9, 1, 12, 7, 6]
console.log(quickSort(arr));
console.log(arr);

Related

  1. swap(i, j)
    Array.prototype.swap = function(i, j){
      var temp = this[i]
      this[i] = this[j]
      this[j] = temp
    
  2. swap(i, j)
    Array.prototype.swap = function (i, j) {
        var k = this[i]; this[i] = this[j]; this[j] = k;
    function bubbleSort(list) {
        var items = list.slice(0), swapped = false, p, q;
        for (p = 1; p < items.length; ++p) {
            for (q = 0; q < items.length - p; ++q) {
                if (items[q + 1] < items[q]) {
                    items.swap(q, q + 1);
    ...
    
  3. swap(i, j)
    'use strict';
    Array.prototype.swap = function (i, j) {
      const temp = this[i];
      this[i] = this[j];
      this[j] = temp;
    function qsort (arr, lo=0, hi=arr.length - 1) {
      if (lo >= hi) return;
      arr.swap(lo, Math.floor(Math.random() * (hi - lo)) + lo);
    ...
    
  4. swap(i, j)
    Array.prototype.swap = function (i, j) {
        var tmp = this[i];
        this[i] = this[j];
        this[j] = tmp;
    };
    
  5. swap(i,j)
    Array.prototype.swap = function(i,j){
      var a = this;
      if(i!=j){
        a[j]^=a[i];
        a[i]^=a[j];
        a[j]^=a[i];
    
  6. swap(index1, index2)
    Array.prototype.swap = function(index1, index2) {
        var temp = this[index1];
        this[index1] = this[index2];
        this[index2] = temp;
    };
    
  7. swap(index1, index2)
    Array.prototype.swap = function(index1, index2) { 
      let temp = this[index1]; 
      this[index1] = this[index2]; 
      this[index2] = temp 
    };
    
  8. swap(indexA, indexB)
    export default function stringPermutation(string) {
      let permutations = []
      function recursivePermutations(array, index) {
        if(index === array.length) {
          permutations.push(array.join(''))
          return
        for (let i = index; i < array.length; i++) {
          let copy = array.slice(0)
    ...
    
  9. swap(index_A, index_B)
    Array.prototype.swap = function(index_A, index_B) {
        var input = this;
        var temp = input[index_A];
        input[index_A] = input[index_B];
        input[index_B] = temp;