Nodejs Array Swap swap(a, b)

Here you can find the source of swap(a, b)

Method Source Code

Array.prototype.swap=function(a, b)
{
   var tmp=this[a];
   this[a]=this[b];//from   w  w  w .j  av  a2s .c  o m
   this[b]=tmp;
}

function insert(array, begin, end, v)
{
   while(begin+1<end && array[begin+1]<v) {
      array.swap(begin, begin+1);
      ++begin;
   }
   array[begin]=v;
}

function merge(array, begin, begin_right, end)
{
   for(;begin<begin_right; ++begin) {
      if(array[begin]>array[begin_right]) {
         var v=array[begin];
         array[begin]=array[begin_right];
         insert(array, begin_right, end, v);
      }
   }
}

function msort(array, begin, end)
{
   var size=end-begin;
   if(size<2) return;

   var begin_right=begin+Math.floor(size/2);

   msort(array, begin, begin_right);
   msort(array, begin_right, end);
   merge(array, begin, begin_right, end);
}

function merge_sort(array)
{
   msort(array, 0, array.length);
}

function dosort(form)
{
   var array=form.unsorted.value.split(/ +/);

   merge_sort(array);

   form.sorted.value=array.join(' ');


}

Related

  1. swap(a, b)
    Array.prototype.swap = function(a, b){
        this[a] = this.splice(b, 1, this[a])[0];
        return this;
    };
    
  2. swap(a, b)
    Array.prototype.swap = function(a, b)
      var t = this[a];
      this[a] = this[b];
      this[b] = t;
    };
    
  3. swap(a, b)
    function qsort(array, begin, end)
      if(end-1>begin) {
        var pivot= 0;
        pivot=partition(array, begin, end, pivot);
        qsort(array, begin, pivot);
        qsort(array, pivot+1, end);
    Array.prototype.swap=function(a, b)
      var tmp=this[a];
      this[a]=this[b];
      this[b]=tmp;
    function partition(array, begin, end, pivot)
      var piv=array[pivot].minPoint.z;
      array.swap(pivot, end-1);
      var store=begin;
      var ix;
      for(ix=begin; ix<end-1; ++ix) {
        if(array[ix].minPoint.z<=piv) {
          array.swap(store, ix);
          ++store;
      array.swap(end-1, store);
      return store;
    function quick_sort(array)
      qsort(array, 0, array.length);
    
  4. 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);
    ...
    
  5. swap(a, b)
    Array.prototype.swap=function(a, b)
        console.log("calling swap");
        var tmp=this[a];
        this[a]=this[b];
        this[b]=tmp;
    
  6. swap(a,b)
    Array.prototype.swap = function(a,b){
      var tmp = this[a];
      this[a] = this[b];
      this[b] = tmp;
    
  7. 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) {
    ...
    
  8. swap(i, j)
    Array.prototype.swap = function(i, j) {
      var a = this[i];
      this[i] = this[j];
      this[j] = a;
    
  9. swap(i, j)
    Array.prototype.swap = function(i, j){
      var temp = this[i]
      this[i] = this[j]
      this[j] = temp