-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathquicksort.js
97 lines (88 loc) · 3.25 KB
/
quicksort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @module lib/quicksort
* @license MIT Copyright 2014 Daniel Imms (http://www.growingwiththeweb.com)
*/
'use strict';
var attachObserver = require('./common/attach-observer');
var defaultSwap = require('./common/default-swap');
var exposeCompareObserver = require('./common/expose-compare-observer');
var exposeSwapObserver = require('./common/expose-swap-observer');
var wrapCompare = require('./common/wrap-compare');
/**
* Sorts an array using quicksort.
* @param {Array} array The array to sort.
* @param {number} left The index to sort from.
* @param {number} right The index to sort to.
* @param {function} compare The compare function.
* @param {function} swapObserver A function to call when the swap operation is
* performed. This can be used to listen in on internals of the algorithm.
* @returns The sorted array.
*/
function sort(array, left, right, compare, swap) {
if (left < right) {
var pivot = partitionRandom(array, left, right, compare, swap);
sort(array, left, pivot - 1, compare, swap);
sort(array, pivot + 1, right, compare, swap);
}
return array;
}
/**
* Partition the array by selecting a random pivot and moving all elements less
* than the pivot to a lesser index and all elements greater than the pivot to a
* greater index.
* @param {Array} array The array to sort.
* @param {number} left The index to sort from.
* @param {number} right The index to sort to.
* @param {function} compare The compare function.
* @param {function} swapObserver A function to call when the swap operation is
* performed. This can be used to listen in on internals of the algorithm.
* @returns The pivot.
*/
function partitionRandom(array, left, right, compare, swap) {
var pivot = left + Math.floor(Math.random() * (right - left));
if (pivot !== right) {
swap(array, right, pivot);
}
return partitionRight(array, left, right, compare, swap);
}
/**
* Partition the array using the right most element as the pivot by moving all
* elements less than the pivot to a lesser index and all elements greater than
* the pivot to a greater index.
* @param {Array} array The array to sort.
* @param {number} left The index to sort from.
* @param {number} right The index to sort to.
* @param {function} compare The compare function.
* @param {function} swapObserver A function to call when the swap operation is
* performed. This can be used to listen in on internals of the algorithm.
* @returns The pivot.
*/
function partitionRight(array, left, right, compare, swap) {
var mid = left;
for (var i = mid; i < right; i++) {
if (compare(array, i, right) <= 0) {
if (i !== mid) {
swap(array, i, mid);
}
mid++;
}
}
if (right !== mid) {
swap(array, right, mid);
}
return mid;
}
/**
* Sorts an array using quicksort.
* @param {Array} array The array to sort.
* @param {function} customCompare A custom compare function.
* @returns The sorted array.
*/
function sortExternal(array, customCompare) {
var compare = wrapCompare(customCompare, sortExternal.compareObserver);
var swap = attachObserver(defaultSwap, sortExternal.swapObserver);
return sort(array, 0, array.length - 1, compare, swap);
};
exposeCompareObserver(sortExternal);
exposeSwapObserver(sortExternal);
module.exports = sortExternal;