Javascript Array exchange(i1,i2)
var getRandomIntInclusive = require('./helpers').getRandomIntInclusive; Array.prototype.exchange = function(i1,i2) { var tmp = this[i1]; this[i1] = this[i2];/*from w w w. j av a 2 s . c o 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;