Here you can find the source of qsort(array, begin, end)
Array.prototype.swap=function(a, b) { var tmp=this[a]; this[a]=this[b];//ww w. ja v a2 s. co 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 */
Array.prototype.quick_sort = function () { if (this.length < 2) { return this; } var pivot = this[Math.round(this.length / 2)]; return this.filter(x => x < pivot) .quick_sort() .concat(this.filter(x => x == pivot)) .concat(this.filter(x => x > pivot).quick_sort()); };
Array.prototype.quick_sort = function () { if (this.length < 2) { return this; } var pivot = this[Math.round(this.length / 2)]; return this.filter(x => x < pivot) .quick_sort() .concat(this.filter(x => x === pivot)) .concat(this.filter(x => x > pivot).quick_sort()); };
Array.prototype.quick_sort = function () if (this.length <= 1) return this; var pivot = this[Math.round(this.length / 2)]; return this.filter(function (x) { return x < pivot }).quick_sort().concat( this.filter(function (x) { return x == pivot })).concat( this.filter(function (x) { return x > pivot }).quick_sort());
Array.prototype.quick_sort = function () { if (this.length < 2) { return this; } var pivot = this[Math.floor(Math.random() * this.length)]; return this.filter(x => x < pivot) .quick_sort() .concat(this.filter(x => x == pivot)) .concat(this.filter(x => x > pivot).quick_sort()); };
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); ...
$(function ($) { function dosort(form) var array=form.unsorted.value.split(/ +/); quick_sort(array); form.sorted.value=array.join(' '); function quick_sort(array) qsort(array, 0, array.length); 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 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; Array.prototype.swap=function(a, b) var tmp=this[a]; this[a]=this[b]; this[b]=tmp; });
Array.prototype.swap=function swapf(a, b) var tmp=this[a]; this[a]=this[b]; this[b]=tmp; function partition(array, begin, end, pivot) var piv=array[pivot]; ...
var arr = [2, 4, 6, 1, 3, 5, 8, 7]; 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); return array; 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; console.log(qsort(arr, 0, arr.length));
Array.prototype.qsort = function() if (this.length < 2) return this; var head = this.shift(); return unpack(function(low, high) { return [].concat(low.qsort(), head, high.qsort()); }, this.partition(function(_) { return _ < head; })); }; ...