Javascript Array swap(a, b)
'use strict';//from www . j av a 2s.c om Array.prototype.swap = function swap (a, b) { const temp = this[a]; this[a] = this[b]; this[b] = temp; }; function insertion_sort (arr) { for (let i = 0; i < arr.length; i++) { let j = i; while (j > 0 && arr[j-1] > arr[j]) arr.swap(j, --j); } } let testArr = []; for (let i = 0; i < 100; i++) { testArr.push(Math.floor(Math.random() * 1000)); } insertion_sort(testArr); console.log(testArr);
Array.prototype.swap = function(a, b){ this[a] = this.splice(b, 1, this[a])[0]; return this;// ww w. ja v a2 s . c o m };
Array.prototype.swap = function(a, b) {
this[a] = [this[b], this[b] = this[a]][0];
}
/* /*from www . j a va 2 s .co m*/ * Global built-in extensions */ /* add swap to array */ Array.prototype.swap = function (a, b) { var tmp = this[a]; this[a] = this[b]; this[b] = tmp; };
Array.prototype.swap = function(a, b) { var t = this[a]; this[a] = this[b];/*from w w w.ja v a 2 s . com*/ this[b] = t; };
Array.prototype.swap = function(a, b) { return [this[a], this[b]] = [this[b], this[a]] }
'use strict';/*from ww w.j a v a 2s .c om*/ Array.prototype.swap = function swap (a, b) { const temp = this[a]; this[a] = this[b]; this[b] = temp; }; function selection_sort (arr) { for (let i = 0; i < arr.length; i++) { let min = i; for (let j = i + 1; j < arr.length; j++) { if (arr[j] < arr[min]) min = j; } arr.swap(i, min); } } let testArr = []; for (let i = 0; i < 100; i++) { testArr.push(Math.floor(Math.random() * 1000)); } selection_sort(testArr); console.log(testArr);
function partition(array, begin, end, pivot) { var piv=array[pivot]; array.swap(pivot, end-1);/*w w w. j av a 2 s . com*/ var store=begin; var ix; for(ix=begin; ix<end-1; ++ix) { if(array[ix]<=piv) { array.swap(store, ix); ++store; } } array.swap(end-1, store); return store; } Array.prototype.swap=function(a, b) { var tmp=this[a]; this[a]=this[b]; this[b]=tmp; } function qsort(array, begin, end) { if(end-1>begin) { var pivot=begin+Math.floor(Math.random()*(end-begin)); pivot=partition(array, begin, end, pivot); qsort(array, begin, pivot); qsort(array, pivot+1, end); } } function quick_sort(array) { qsort(array, 0, array.length); }
function qsort(array, begin, end) { if(end-1>begin) { var pivot= 0;//from w ww . ja v a2 s . c o m 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); }
// Returns last element in array, or last element that matches a predicate fn Array.prototype.swap = function(a, b) { // Prototypes throw TypeErrors when the context or arguments are invalid if (Object.prototype.toString.call(this) !== '[object Array]') { throw new TypeError("`this` must be Array, not " + typeof this); }// w w w .j a v a2s . co m 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); } var temp = this[a]; this[a] = this[b]; this[b] = temp; return this; };
Array.prototype.swap=function(a, b) { var tmp=this[a]; this[a]=this[b];//from ww w . java 2 s.c o m this[b]=tmp; } function partition(array, begin, end, pivot) { var piv=array[pivot]; array.swap(pivot, end-1); var store=begin; var ix; for(ix=begin; ix<end-1; ++ix) { if(array[ix]<=piv) { array.swap(store, ix); ++store; } } array.swap(end-1, store); return store; } function qsort(array, begin, end) { if(end-1>begin) { var pivot=begin+Math.floor(Math.random()*(end-begin)); pivot=partition(array, begin, end, pivot); qsort(array, begin, pivot); qsort(array, pivot+1, end); } } function quick_sort(array) { qsort(array, 0, array.length); } arr = "CFVGBHNJMKA".split(""); // [14,12,3,2,1] quick_sort(arr) arr /* 1,2,3,12,14 */ /* B,C,F,G,H,J,K,M,N,V */ /* A,B,C,F,G,H,J,K,M,N,V */