Nodejs Array Swap swap(index_A, index_B)

Here you can find the source of swap(index_A, index_B)

Method Source Code

// this approach is pretty bad but I wanted the end-result code
// to be more readable
// I'll probably switch this back later to better match js best practices

Array.prototype.swap = function(index_A, index_B) {
    var input = this;

    var temp = input[index_A];
    input[index_A] = input[index_B];//w  w w  . j  a  v a  2 s  .c  o  m
    input[index_B] = temp;
}

Related

  1. 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];
    
  2. swap(idx1, idx2)
    function quickSort(arr){
        function partition(low, high){
            if(high - low < 2){
                return
            var pivot = arr[low]
            var wall = low
            for(var i = low; i <= high; i++){
                if(arr[i] < pivot){
    ...
    
  3. swap(index1, index2)
    Array.prototype.swap = function(index1, index2) {
        var temp = this[index1];
        this[index1] = this[index2];
        this[index2] = temp;
    };
    
  4. swap(index1, index2)
    Array.prototype.swap = function(index1, index2) { 
      let temp = this[index1]; 
      this[index1] = this[index2]; 
      this[index2] = temp 
    };
    
  5. 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)
    ...
    
  6. swap(one, two)
    Array.prototype.swap = function (one, two) {
      var tmp = this[one];
      this[one] = this[two];
      this[two] = tmp;
    function randInt (low, high) {
      return Math.floor(Math.random() * (high - low + 1)) + low;
    function randChar () {
    ...
    
  7. swap(pos1, pos2)
    Array.prototype.swap = function(pos1, pos2) {
      var temp = this[pos1];
      this[pos1] = this[pos2];
      this[pos2] = temp;
    
  8. swap(sIdx, tIdx )
    ;"use strict";
    var log=function(msg){console.log(msg);};
    var alert=function(msg){log(msg);};
    var arr = [3,6,7,5,3,6,2,9,1,5,33,-12,0,-122,-Infinity, 125, 33, 55, 77];
    Array.prototype.swap = function (sIdx, tIdx ) {
      var tmp = this[sIdx];
      this[sIdx] = this[tIdx];
      this[tIdx] = tmp; 
    Array.prototype.quickSort = function () {
      function _quickSort(arr, leftIdx, rightIdx) {
        if(leftIdx < rightIdx) {
          var pivot = arr[leftIdx],
          chkLeftIdx = leftIdx,
          chkRightIdx = rightIdx;
          while(chkLeftIdx < chkRightIdx) {
            while(arr[chkRightIdx] > pivot) {
              chkRightIdx--;
            while(chkLeftIdx < chkRightIdx && arr[chkLeftIdx] <= pivot) {
              chkLeftIdx++;
            arr.swap(chkLeftIdx, chkRightIdx);
          arr.swap(leftIdx, chkLeftIdx);
          _quickSort(arr, leftIdx, chkLeftIdx-1);
          _quickSort(arr, chkLeftIdx+1, rightIdx);
        return arr;
      return _quickSort(this, 0, this.length -1);
    };
    console.log(arr.sort());
    console.log(arr.quickSort());
    
  9. swap(x, y)
    Array.prototype.swap = function (x, y) {
        var b = this[x];
        this[x] = this[y];
        this[y] = b;
        return this;
    };