-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinsertion-sort.js
49 lines (43 loc) · 1.37 KB
/
insertion-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
/**
* @module lib/insertion-sort
* @license MIT Copyright 2014 Daniel Imms (http://www.growingwiththeweb.com)
*/
'use strict';
var attachObserver = require('./common/attach-observer');
var defaultCompare = require('./common/default-compare');
//var exposeCompareObserver = require('./common/expose-compare-observer');
var exposeShiftObserver = require('./common/expose-shift-observer');
/**
* Sorts an array using insertion sort.
* @param {Array} array The array to sort.
* @param {function} compare The compare function.
* @returns The sorted array.
*/
function sort(array, compare) {
for (var i = 1; i < array.length; i++) {
var item = array[i];
var indexHole = i;
while (indexHole > 0 && compare(array[indexHole - 1], item) > 0) {
array[indexHole] = array[--indexHole];
}
array[indexHole] = item;
if (sortExternal.shiftObserver) {
sortExternal.shiftObserver(i, indexHole);
}
}
return array;
}
/**
* Sorts an array.
* @param {Array} array The array to sort.
* @param {function} customCompare A custom compare function.
* @returns The sorted array.
*/
function sortExternal(array, customCompare) {
var compare = customCompare || defaultCompare;
return sort(array, compare);
};
// TODO: Re-enable when it works with shifted elements
//exposeCompareObserver(sortExternal);
exposeShiftObserver(sortExternal);
module.exports = sortExternal;