-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcomb-sort.js
59 lines (50 loc) · 1.63 KB
/
comb-sort.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
/**
* @module lib/comb-sort
* @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 comb sort.
* @param {Array} array The array to sort.
* @param {function} compare The compare function.
* @param {function} swap 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, compare, swap) {
var gap = array.length;
var shrinkFactor = 1.3;
var swapped;
while (gap > 1 || swapped) {
if (gap > 1) {
gap = Math.floor(gap / shrinkFactor);
}
swapped = false;
for (var i = 0; gap + i < array.length; ++i) {
if (compare(array, i, i + gap) > 0) {
swap(array, i, i + gap);
swapped = true;
}
}
}
return array;
}
/**
* Sorts an array using comb sort.
* @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, compare, swap);
};
exposeCompareObserver(sortExternal);
exposeSwapObserver(sortExternal);
module.exports = sortExternal;