Javascript Array swap(i, j)

Description

Javascript Array swap(i, j)


Array.prototype.swap = function(i, j) {
  var a = this[i];
  this[i] = this[j];/*from  www  .ja v  a 2s  .c o m*/
  this[j] = a;
}


// function output

Javascript Array swap(i, j)

Array.prototype.swap = function(i, j){
 var temp = this[i]
 this[i] = this[j]//from  www .j a v a2 s  .co  m
 this[j] = temp
}

Javascript Array 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);// w  ww  . j a  v a 2  s  .  c o m
                swapped =true;
            }
        }
        if (!swapped) break;
    }
    return items;
}

var N = 100, data = []; while (N > 0) data.push(N--);
bubbleSort(data);

Javascript Array swap(i, j)

'use strict';/* w  w  w . ja  va  2s  .c om*/

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);

  const pivot     = arr[lo];
  let   swapPoint = lo;

  for (let i = lo + 1; i <= hi; i++) {
    if (arr[i] < pivot) arr.swap(i, ++swapPoint);
  }
  arr.swap(lo, swapPoint);

  qsort(arr, lo, swapPoint - 1);
  qsort(arr, swapPoint + 1, hi);

  // return arr;
}

const testArr = [5,2,78,5,0,13,6];
qsort(testArr);
console.log(testArr);

Javascript Array swap(i, j)

Array.prototype.swap = function (i, j) {
    var k = this[i]; this[i] = this[j]; this[j] = k;
}


function gen_data ()
{
  var data = [];//from  ww  w.  j  av a  2s. c  o  m
  var N = 100;
  while (N > 0) data.push(N--);

  return data;
}


function bubbleSort() {
    var list = gen_data ();
    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);
                swapped =true;
            }
        }
        if (!swapped) break;
    }
    return items;
}

setTimeout (bubbleSort, 0);
setTimeout (bubbleSort, 0);

Javascript Array swap(i, j)

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

array = [3,2,1, 5]

Array.prototype.swap  = function(i, j) {
  var temp = this[i];
  this[i] = this[j];
  this[j] = temp;
};

Array.prototype.bubbleSort = function() {
  var sorted = true

  do {
    sorted = true;
    for (var i = 0; i < this.length-1; i ++) {
      if (this[i] > this[i+1]) {
        this.swap(i, i+1)
        sorted = false;
      }
    };
  } while (!sorted)
  return this;

};


String.prototype.mySubstrings = function() {
  var res = [];
  for (var i = 0; i < this.length; i++) {
    for (var j = i + 1; j<= this.length; j++) {
      var substring = this.slice(i,j)
      if (res.indexOf(substring) === -1) {
        res.push(substring)
      }
    }
  }

  return res;
};

Javascript Array swap(i, j)

/**//from   w ww  . j  av a 2s .c om
 * QuickSort Javascript implementation
 *  - Pivot: choosing the middle index of the partition
 *  - Best: nlog(n) / Average: nlog(n) / Worst: n^2
 *  - Memory: log(n) / Not stable
 *
 * @author: Sebastian Kim
 */

Array.prototype.swap = function(i, j) {
  var tmp = this[i];
  this[i] = this[j];
  this[j] = tmp;
}

function partition(A, p, q, r) {
  var pivot = A[r];
  A.swap(r, p);
  var i = p;

  for(var j = p + 1; j <= q; ++j) {
    if(A[j] <= pivot) {
      A.swap(++i, j);
    }
  }
  A.swap(p, i);

  return i;
}


function quicksort(A, p, q)
{
  if(p < q) {
    var r = Math.floor(p + (q - p) / 2); // Prevent integer overflow
    r = partition(A, p, q, r);
    quicksort(A, p, r - 1);
    quicksort(A, r + 1, q);
  }

  return A;
}

Array.prototype.quickSort = function() {
  return quicksort(this, 0, this.length - 1);
}

Javascript Array swap(i, j)

/////  w ww.  j av a 2s  . co  m
Array.prototype.swap = function(i, j) {
 var tmp = this[i];
 this[i] = this[j];
 this[j] = tmp;
}

Javascript Array swap(i,j)


module.exports = bubbleSort;//from www  .  ja  v  a2 s  .  com

Array.prototype.swap = function(i,j) {
  var temp = this[i];
  this[i] = this[j];
  this[j] = temp;
};

function bubbleSort(array) {
  for (var i = 0; i < array.length-1; i++) {
    for (var j = 0; j < array.length-1; j++) {
      if (array[j] > array[j+1]) {
        array.swap(j, j+1);
      }
    }
  }
  return array;
}

Javascript Array swap(i,j)

Array.prototype.swap = function(i,j) {
  var temp = this[i];
  this[i] = this[j];/*  w  w w . j  av  a2 s .  co  m*/
  this[j] = temp;
};

var insertionSort = module.exports = function(array) {
  for (var i = 0; i < array.length; i++) {
    for (var j = i-1; j >= 0; j--) {
      if (array[j] > array[j+1]) {
        array.swap(j, j+1);
      }
    }
  }
  return array;
};

Javascript Array swap(i,j)

Array.prototype.swap = function (i,j) {
    i = i || 0;/*from   w ww.ja  v  a  2 s  .c  o m*/
    j = j || i+1;
    if (i >= this.length - 1 || j >= this.length || i < 0 || j < 0) throw new Error("index out of range");
    var temp = this[i];
    this[i] = this[j];
    this[j] = temp;
}

function bubbleSort(arr) {
    var swapCount;
    for (var i = arr.length - 1; i >= 0 && swapCount !== 0; i--) {
        swapCount = 0;
        for (var j = 0; j < i; j++) {
            if (compare(arr[j], arr[j + 1])) {
                arr.swap(j);
                swapCount++;
            }
        }
    }
    return arr;
}

function compare(a, b) {
    return a > b;
}



PreviousNext

Related