UNPKG

4.67 kBJavaScriptView Raw
1var Sort = (function () {
2 function Sort() {
3 }
4 Sort.selectionSort = function (arr, key) {
5 if (!arr || !arr.length) {
6 return arr;
7 }
8 var len = arr.length;
9 var minIndex;
10 for (var i = 0; i < len; i++) {
11 minIndex = i;
12 for (var j = i; j < len; j++) {
13 var start = arr[minIndex], current = arr[j];
14 var condition = key ? start[key] > current[key] : start > current;
15 if (condition) {
16 minIndex = j;
17 }
18 }
19 this.swap(arr, i, minIndex);
20 }
21 return arr;
22 };
23 Sort.insertSort = function (arr, key) {
24 if (!arr || !arr.length) {
25 return arr;
26 }
27 var len = arr.length;
28 for (var i = 1; i < len; i++) {
29 var j = i;
30 while (j > 0) {
31 var current = arr[j], prev = arr[j - 1];
32 var condition = key ? current[key] < prev[key] : current < prev;
33 if (condition)
34 this.swap(arr, j, j - 1);
35 j--;
36 }
37 }
38 ;
39 return arr;
40 };
41 Sort.bubbleSort = function (arr, key) {
42 if (!arr || !arr.length) {
43 return arr;
44 }
45 ;
46 var len = arr.length;
47 for (var i = 0; i < len - 1; i++) {
48 for (var j = 0; j < len - 1 - i; j++) {
49 var condition = key ? arr[j][key] > arr[j + 1][key] : arr[j] > arr[j + 1];
50 if (condition) {
51 this.swap(arr, j, j + 1);
52 }
53 }
54 }
55 return arr;
56 };
57 Sort.shellSort = function (arr, key) {
58 if (!arr || !arr.length) {
59 return arr;
60 }
61 ;
62 var len = arr.length;
63 var i = 1;
64 while (Math.floor(len / (2 * i))) {
65 var quotient = Math.floor(len / (2 * i));
66 for (var j = 0; j < 2 * i; j++) {
67 var currentIndex = j;
68 while (currentIndex + quotient < len) {
69 var current = arr[currentIndex], quotientNext = arr[currentIndex + quotient];
70 var condition = key ? quotientNext[key] < current[key] : quotientNext < current;
71 if (condition)
72 this.swap(arr, currentIndex, currentIndex + quotient);
73 currentIndex += quotient;
74 }
75 ;
76 }
77 ;
78 i++;
79 }
80 };
81 Sort.mergeSort = function (arr, key) {
82 if (!arr || !arr.length) {
83 return arr;
84 }
85 ;
86 var len = arr.length;
87 if (len < 2) {
88 return arr;
89 }
90 var middle = Math.floor(len / 2), left = arr.slice(0, middle), right = arr.slice(middle);
91 return this.merge(this.mergeSort(left, key), this.mergeSort(right, key), key);
92 };
93 Sort.merge = function (left, right, key) {
94 var result = [];
95 while (left.length > 0 && right.length > 0) {
96 var leftLassThanEqualRight = key ? left[0][key] <= right[0][key] : left[0] <= right[0];
97 if (leftLassThanEqualRight) {
98 result.push(left.shift());
99 }
100 else {
101 result.push(right.shift());
102 }
103 }
104 while (left.length) {
105 result.push(left.shift());
106 }
107 while (right.length) {
108 result.push(right.shift());
109 }
110 return result;
111 };
112 Sort.quickSort = function (arr, key) {
113 sort(arr, 0, arr.length - 1);
114 return arr;
115 function sort(arr, first, last) {
116 if (first >= last) {
117 return;
118 }
119 var low = first;
120 var high = last;
121 var middleValue = arr[first];
122 while (first < last) {
123 while (first < last && Sort.compare(arr[last], middleValue)) {
124 --last;
125 }
126 arr[first] = arr[last];
127 while (first < last && !Sort.compare(arr[first], middleValue)) {
128 ++first;
129 }
130 arr[last] = arr[first];
131 }
132 arr[first] = middleValue;
133 sort(arr, low, first - 1);
134 sort(arr, first + 1, high);
135 }
136 };
137 Sort.compare = function (a, b, key) {
138 if (key) {
139 return a[key] >= b[key];
140 }
141 return a > b;
142 };
143 Sort.swap = function (arr, i, j) {
144 var temp = arr[i];
145 arr[i] = arr[j];
146 arr[j] = temp;
147 };
148 return Sort;
149}());
150export { Sort };