You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
961 B
33 lines
961 B
8 months ago
|
var swap = require('./swap');
|
||
|
var isSorted = require('./isSorted');
|
||
|
exports = function(arr) {
|
||
|
var cmp =
|
||
|
arguments.length > 1 && arguments[1] !== undefined
|
||
|
? arguments[1]
|
||
|
: isSorted.defComparator;
|
||
|
return quickSort(arr, 0, arr.length - 1, cmp);
|
||
|
};
|
||
|
function quickSort(arr, left, right, cmp) {
|
||
|
if (arr.length <= 1) return arr;
|
||
|
var idx = partition(arr, left, right, cmp);
|
||
|
if (left < idx - 1) quickSort(arr, left, idx - 1, cmp);
|
||
|
if (idx < right) quickSort(arr, idx, right, cmp);
|
||
|
return arr;
|
||
|
}
|
||
|
function partition(arr, left, right, cmp) {
|
||
|
var pivot = arr[floor((right + left) / 2)];
|
||
|
while (left <= right) {
|
||
|
while (cmp(arr[left], pivot) < 0) left++;
|
||
|
while (cmp(arr[right], pivot) > 0) right--;
|
||
|
if (left <= right) {
|
||
|
swap(arr, left, right);
|
||
|
left++;
|
||
|
right--;
|
||
|
}
|
||
|
}
|
||
|
return left;
|
||
|
}
|
||
|
var floor = Math.floor;
|
||
|
|
||
|
module.exports = exports;
|