Nodejs Array Swap swap(x, y)

Here you can find the source of swap(x, y)

Method Source Code

"use strict";//from  w  ww  .  ja v a 2s .c o m

function Median(A) {
    var n = A.length;
    if (n == 1) {
        return A[0];
    } else {
        return Select(A, 0, Math.floor(n / 2), n - 1);
    }
}

function Select(A, l, m, h) {
    var pos = Partition(A, l, h);
    if (pos == m) {
        return A[pos];
    } else if (pos > m) {
        return Select(A, l, m, pos - 1);
    } else if (pos < m) {
        return Select(A, pos + 1, m, h);
    }
}

function Partition(A, l, h) {
    var pivotval = A[l];
    var pivotloc = l;

    var j;

    for (j = l + 1; j <= h; j = j + 1) {
        if (A[j] < pivotval) {
            pivotloc = pivotloc + 1;
            A.swap(pivotloc, j);
        }
    }
    A.swap(l, pivotloc);
    return pivotloc;
}


// simple array element swap implementation
Array.prototype.swap = function (x, y) {
    var tmp = this[x];
    this[x] = this[y];
    this[y] = tmp;
    return this;
};


module.exports = Median;

Related

  1. 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 () {
    ...
    
  2. swap(pos1, pos2)
    Array.prototype.swap = function(pos1, pos2) {
      var temp = this[pos1];
      this[pos1] = this[pos2];
      this[pos2] = temp;
    
  3. 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());
    
  4. swap(x, y)
    Array.prototype.swap = function (x, y) {
        var b = this[x];
        this[x] = this[y];
        this[y] = b;
        return this;
    };
    
  5. 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;
    
  6. swap(x, y)
    Array.prototype.swap = function(x, y) {
        if (x > this.length || y > this.length || x === y) {
            return
        var tem = this[x];
        this[x] = this[y];
        this[y] = tem;
    
  7. swap(x, y)
    Array.prototype.swap = function(x, y){
      var temp = this[x];
      this[x] = this[y];
      this[y] = temp;
      return this;
    
  8. swapArray(index_a, index_b)
    RegExp.escape = function(s) {
        return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    };
    Array.prototype.swapArray = function(index_a, index_b) {
        var temp = this[index_a];
        this[index_a] = this[index_b];
        this[index_b] = temp;
    
  9. swapByIndex( i1, i2 )
    Array.prototype.swapByIndex = function( i1, i2 ) {
      var e1 = null, e2 = null;
      i1 = checkFn( i1 );
      i2 = checkFn( i2 );
      if( typeof i1 === 'string' && i1 !== '' ) e1 = i1.toNumber();
      if( typeof i2 === 'string' && i2 !== '' ) e2 = i2.toNumber();
      if( e1 === -1 || this[i1] === undefined ) { throw new Error( 'No element with index \'' + i1 + '\' was found in array.' ); return; }
      if( e2 === -1 || this[i2] === undefined ) { throw new Error( 'No element with index \'' + i2 + '\' was found in array.' ); return; }
      var e = this[i1];
    ...