Nodejs Array Swap swap(one, two)

Here you can find the source of swap(one, two)

Method Source Code

// Swap two entries in an array
Array.prototype.swap = function (one, two) {
   var tmp = this[one];
   this[one] = this[two];/*from w w  w  .j a v  a 2 s.  co m*/
   this[two] = tmp;
}

// Generate a random integer in [low, high]
function randInt (low, high) {
   return Math.floor(Math.random() * (high - low + 1)) + low;
}

// Returns a random character in [A-Za-z0-9]
function randChar () {
   var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
   return letters.substr(randInt(0, letters.length-1), 1);
}

Related

  1. 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){
    ...
    
  2. swap(index1, index2)
    Array.prototype.swap = function(index1, index2) {
        var temp = this[index1];
        this[index1] = this[index2];
        this[index2] = temp;
    };
    
  3. swap(index1, index2)
    Array.prototype.swap = function(index1, index2) { 
      let temp = this[index1]; 
      this[index1] = this[index2]; 
      this[index2] = temp 
    };
    
  4. 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)
    ...
    
  5. 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;
    
  6. swap(pos1, pos2)
    Array.prototype.swap = function(pos1, pos2) {
      var temp = this[pos1];
      this[pos1] = this[pos2];
      this[pos2] = temp;
    
  7. 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());
    
  8. swap(x, y)
    Array.prototype.swap = function (x, y) {
        var b = this[x];
        this[x] = this[y];
        this[y] = b;
        return this;
    };
    
  9. swap(x, y)
    function MathDist(xo, yo, x, y) {
      return Math.sqrt((x-xo)*(x-xo) + (y-yo)*(y-yo));
    function MathVecNorm(vx, vy) {
      return Math.sqrt(vx*vx + vy*vy);
    function MathDotProduct(a1, a2, b1, b2) {
      return a1*b1 + a2*b2;
    function MathSign(n) {
      return (n >= 0) ? 1 : -1;
    const MAX_RADIUS = 50.0;
    const MIN_RADIUS = 5;
    const MAX_DENSITY = 8.00 * 1/1963.495;
    const MIN_DENSITY =  0.50 * 1/1963.495;
    Array.prototype.swap = function (x, y) {
      var b = this[x];
      this[x] = this[y];
      this[y] = b;
      return this;
    function NewArray2d(rows, columns) {
       var array = new Array(rows);
       for (var i = 0; i < rows; i++) {
           array[i] = new Array(columns);
       return array;