Nodejs Array Swap swap(i, j)

Here you can find the source of swap(i, j)

Method Source Code

Array.prototype.swap = function(i, j) {
   var a = this[i];
   this[i] = this[j];/*  w  w  w.j a  v  a  2  s  .c  om*/
   this[j] = a;
}


// function output

Related

  1. swap(a, b)
    Array.prototype.swap = function(a, b) {
        if (Object.prototype.toString.call(this) !== '[object Array]') {
            throw new TypeError("`this` must be Array, not " + typeof this);
        if (typeof a !== 'number') {
            throw new TypeError("argument[0] must be number, not " + typeof a);
        if (typeof b !== 'number') {
            throw new TypeError("argument[1] must be number, not " + typeof b);
    ...
    
  2. swap(a, b)
    Array.prototype.swap=function(a, b)
        console.log("calling swap");
        var tmp=this[a];
        this[a]=this[b];
        this[b]=tmp;
    
  3. swap(a, b)
    Array.prototype.swap=function(a, b)
      var tmp=this[a];
      this[a]=this[b];
      this[b]=tmp;
    function insert(array, begin, end, v)
      while(begin+1<end && array[begin+1]<v) {
    ...
    
  4. swap(a,b)
    Array.prototype.swap = function(a,b){
      var tmp = this[a];
      this[a] = this[b];
      this[b] = tmp;
    
  5. swap(firstIndex, secondIndex)
    function bubbleSort(arr) {
      if (arr.length === 0 || arr.length === undefined) {
        return "No array as arg!"
      if (arr.length === 1) {
        return arr;
      var swapped = true;
      while (swapped) {
    ...
    
  6. swap(i, j)
    Array.prototype.swap = function(i, j){
      var temp = this[i]
      this[i] = this[j]
      this[j] = temp
    
  7. swap(i, j)
    Array.prototype.swap = function (i, j) {
        var k = this[i]; this[i] = this[j]; this[j] = k;
    function bubbleSort(list) {
        var items = list.slice(0), swapped = false, p, q;
        for (p = 1; p < items.length; ++p) {
            for (q = 0; q < items.length - p; ++q) {
                if (items[q + 1] < items[q]) {
                    items.swap(q, q + 1);
    ...
    
  8. swap(i, j)
    'use strict';
    Array.prototype.swap = function (i, j) {
      const temp = this[i];
      this[i] = this[j];
      this[j] = temp;
    function qsort (arr, lo=0, hi=arr.length - 1) {
      if (lo >= hi) return;
      arr.swap(lo, Math.floor(Math.random() * (hi - lo)) + lo);
    ...
    
  9. swap(i, j)
    Array.prototype.swap = function (i, j) {
        var tmp = this[i];
        this[i] = this[j];
        this[j] = tmp;
    };