massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / core-js / internals / array-sort.js
1 var arraySlice = require('../internals/array-slice-simple');
2
3 var floor = Math.floor;
4
5 var mergeSort = function (array, comparefn) {
6   var length = array.length;
7   var middle = floor(length / 2);
8   return length < 8 ? insertionSort(array, comparefn) : merge(
9     array,
10     mergeSort(arraySlice(array, 0, middle), comparefn),
11     mergeSort(arraySlice(array, middle), comparefn),
12     comparefn
13   );
14 };
15
16 var insertionSort = function (array, comparefn) {
17   var length = array.length;
18   var i = 1;
19   var element, j;
20
21   while (i < length) {
22     j = i;
23     element = array[i];
24     while (j && comparefn(array[j - 1], element) > 0) {
25       array[j] = array[--j];
26     }
27     if (j !== i++) array[j] = element;
28   } return array;
29 };
30
31 var merge = function (array, left, right, comparefn) {
32   var llength = left.length;
33   var rlength = right.length;
34   var lindex = 0;
35   var rindex = 0;
36
37   while (lindex < llength || rindex < rlength) {
38     array[lindex + rindex] = (lindex < llength && rindex < rlength)
39       ? comparefn(left[lindex], right[rindex]) <= 0 ? left[lindex++] : right[rindex++]
40       : lindex < llength ? left[lindex++] : right[rindex++];
41   } return array;
42 };
43
44 module.exports = mergeSort;