forked from blakeembrey/code-problems
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsertion-sort.js
More file actions
44 lines (37 loc) · 1.03 KB
/
Copy pathinsertion-sort.js
File metadata and controls
44 lines (37 loc) · 1.03 KB
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
// insertion-sort
'use strict';
module.exports = function (array, compare) {
// Not an array, empty or array of 1 is already sorted
if (!Array.isArray(array) || array.length < 2) {
return array;
}
// Swap elements of the array
var swap = function (array, first, second) {
var temp = array[first];
array[first] = array[second];
array[second] = temp;
return array;
};
// Create a compare function if one is not passed in
if (typeof compare !== 'function') {
compare = function (a, b) {
return a > b ? 1 : -1;
};
}
var i, j;
/*
* Assume first element is sorted
* Add first unsorted element to sorted array
* Compare new element in sorted array with previous elements
* to determine correct destination index in sorted array
*/
for (i = 1; i < array.length; i++) {
j = i;
// Make sure we don't walk off the array and compare until sorted
while ((j - 1) >= 0 && compare(array[j], array[j - 1]) < 0) {
swap(array, j, j-1);
j--;
}
}
return array;
};