javascript
function qsort(a, comp) { if (typeof comp==="undefined") { comp = function(a, b) { return a - b < 0; } } function _qsort(a, low, high) { if (!comp(low, high)) { return; } var first = low, last = high, pivot = a[first]; while (comp(first, last)) { while (comp(first, last) && !comp(a[last], pivot)) { --last; } a[first] = a[last]; while (comp(first, last) && !comp(pivot, a[first])) { ++first; } a[last] = a[first]; } a[first] = pivot; _qsort(a, low, first-1); _qsort(a, first+1, high); } return _qsort(a, 0, a.length-1); } // quicksort var a = [57, 68, 59, 52, 52, 72, 28, 96, 33, 24]; qsort(a); console.log(a); // [24, 28, 33, 52, 52, 57, 59, 68, 72, 96]
原文地址:https://www.cnblogs.com/mingzhanghui/p/9274431.html
时间: 2024-11-06 17:11:13