List of utility methods to do Array Quick Sort
qsort(array, begin, end)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)); | |
qsort()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; })); }; ... | |
qsort(begin,end)Array.prototype.qsort = function(begin,end){ function swap(array,a,b){ var c = array[a]; array[a] = array[b]; array[b] = c; if(begin < end){ var array = this; var index = begin; ... |