Nodejs Array Exchange exchange(i1,i2)

Here you can find the source of exchange(i1,i2)

Method Source Code

Array.prototype.exchange = function(i1,i2) {
  var tmp = this[i1];
  this[i1] = this[i2];//  w  w  w .  ja  v  a  2s . co m
  this[i2] = tmp;
};

var randomizeInPlace = function(A) {
  // start at i = 1 since at i = 0
  // exchanges A[0] with A[0] and that computation is not needed
  for (var i = 1, n = A.length; i < n; i++){
    A.exchange(i,getRandomIntInclusive(0,i));
  }
  return A;
};

// console.log(randomizeInPlace([1,2,3,4,5]));

module.exports = randomizeInPlace;

Related

  1. exchange(i1,i2)
    Array.prototype.exchange = function(i1,i2) {
      var tmp = this[i1];
      this[i1] = this[i2];
      this[i2] = tmp;
    };