Javascript Array swap(x, y)

Description

Javascript Array swap(x, y)


/*/* w w  w  . j  a v a 2s. co  m*/
 * Project Ariana
 * arrayFunctions.js
 *
 * This file contains a function for using arrays.
 *
 */

Array.prototype.swap = function(x, y) {
    var b = this[x];
    this[x] = this[y];
    this[y] = b;
    return this;
};

Javascript Array swap(x, y)

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

Javascript Array 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;// w w w . j av  a2  s .co m
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;
}

Javascript Array swap(x,y)


Array.prototype.swap = function (x,y) {
  var b = this[x];
  this[x] = this[y];/*ww  w. ja v a 2s  .  c  om*/
  this[y] = b;
  return this;
}

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

bubble_sort([2,4,1,6,5])

Javascript Array swap(x,y)

// A set of base functions that the algorithms make use of

// Adding a method to Array to allow for easy 2-value swapping
Array.prototype.swap = function (x,y) {
  var temp_holder = this[x];
  this[x] = this[y];// ww  w .j  ava2s  . c  om
  this[y] = temp_holder;
  return this;
}

Javascript Array swap(x,y)

Array.prototype.swap = function (x,y) {
  var b = this[x];
  this[x] = this[y];// www.j av  a 2  s  . c  om
  this[y] = b;
  return this;
}

Javascript Array swap(x,y)

Array.prototype.swap = function (x,y) {
  var b = this[x];
  this[x] = this[y];//from w w w .  ja va2 s. c o  m
  this[y] = b;
  return this;
}

function insertion_sort(arr){
   for(var i = 1; i<arr.length; i++){
       var j = i-1;
       while(j>=0 && arr[j] > arr[j+1]){
           arr.swap(j,j+1)
           j--;
       }
   }
   return arr;
}

insertion_sort([5,2,8,1,3])

////////////////////////////
function compare(a,b){
  return a-b;
}

function insertionSort(arr, cmp){
  cmp = cmp || compare;
  var cirrent;
  var j;
  for(var  i = 0; i<arr.length; i++){
    current = arr[i];
    j = i-1;
    while( j >= 0 && cmp(arr[j], current) > 0){
      arr[j+1] = arr[j];
      j--;
    }
    arr[j+1] = current;
  }
  return arr;
}

Javascript Array swap(x,y)

Array.prototype.swap = function(x,y){
    var b = this[x];
    this[x] = this[y];/*from   ww w  .  ja va  2s.  com*/
    this[y] = b;
    return this;
}

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

selection_sort([5,2,7,1,9])


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

function selectionSort(arr, cmp){
  cmp = cmp || compare;
  var min;
  var idx;
  var temp;
  for(var i = 0; i < arr.length; i++){
    idx = i;
    min = arr[i];
    for(var  j = i+1;j<arr.length;j++){
      if(cmp(min,arr[j])>0){
        min = arr[j];
        idx = j;
      }
    }
    temp = arr[i];
    arr[i] = min;
    arr[idx] = temp;
  }
  return arr;
}



PreviousNext

Related