Skip to content

Commit cb405d5

Browse files
committed
refined sortings
1 parent cd6a0e7 commit cb405d5

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/_.general/sorting.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ function checkArray(arr) {
2929
export function bubbleSort(array) {
3030
const arr = array.slice();
3131
checkArray(arr);
32-
let hasSwap = false;
32+
if (arr.length < 2) return arr;
33+
3334
for (let i = arr.length - 1; i > 0; i--) {
35+
let swapped = false;
3436
for (let j = 0; j < i; j++) {
3537
if (arr[j] > arr[j + 1]) {
3638
swap(arr, j, j + 1);
37-
hasSwap = true;
39+
swapped = true;
3840
}
3941
}
40-
if (!hasSwap) {
41-
return arr;
42-
}
42+
if (!swapped) return arr;
4343
}
4444
return arr;
4545
}
@@ -58,10 +58,12 @@ export function bubbleSort(array) {
5858
export function selectionSort(array) {
5959
const arr = array.slice();
6060
checkArray(arr);
61+
if (arr.length < 2) return arr;
62+
6163
for (let i = 0; i < arr.length - 1; i++) {
6264
let min = i;
6365
for (let j = i + 1; j < arr.length; j++) {
64-
if (arr[min] >= arr[j]) {
66+
if (arr[min] > arr[j]) {
6567
min = j;
6668
}
6769
}
@@ -87,14 +89,17 @@ export function selectionSort(array) {
8789
export function insertionSort(array) {
8890
const arr = array.slice();
8991
checkArray(arr);
92+
if (arr.length < 2) return arr;
93+
9094
for (let i = 1; i < arr.length; i++) {
91-
let cur = i;
92-
const curVal = arr[cur];
93-
while (cur > 0 && arr[cur - 1] > curVal) {
94-
arr[cur] = arr[cur - 1];
95-
cur -= 1;
95+
let j = i;
96+
const toInsert = arr[j];
97+
while (j > 0 && arr[j - 1] > toInsert) {
98+
// compare all prev items with the one to insert until find the position to insert
99+
arr[j] = arr[j - 1];
100+
j -= 1;
96101
}
97-
arr[cur] = curVal;
102+
arr[j] = toInsert;
98103
}
99104
return arr;
100105
}
@@ -114,6 +119,9 @@ export function insertionSort(array) {
114119
*/
115120
export function shellSort(array, base = 3) {
116121
const arr = array.slice();
122+
checkArray(arr);
123+
if (arr.length < 2) return arr;
124+
117125
const len = arr.length;
118126
let gap = 1;
119127

@@ -153,12 +161,11 @@ export function shellSort(array, base = 3) {
153161
export function quickSortWithArray(array) {
154162
const arr = array.slice();
155163
checkArray(arr);
156-
if (arr.length < 2) {
157-
return arr;
158-
}
164+
if (arr.length < 2) return arr;
165+
159166
const [pivot, ...rest] = arr;
160167
const larger = rest.filter((item) => item > pivot);
161-
const smaller = rest.filter((item) => item <= pivot);
168+
const smaller = rest.filter((item) => item <= pivot); // includes items equal to pivot
162169

163170
return [...quickSortWithArray(smaller), pivot, ...quickSortWithArray(larger)];
164171
}
@@ -178,7 +185,7 @@ export function quickSortWithArray(array) {
178185
*/
179186
export function quickSort(array, lo = 0, hi = array.length) {
180187
checkArray(array);
181-
if (array.length <= 1 || hi <= lo) return array;
188+
if (array.length < 2 || hi - 1 <= lo) return array;
182189

183190
const partition = (arr, left, right) => {
184191
let i = left;
@@ -234,7 +241,7 @@ export function mergeSort(array) {
234241
return result.concat(left.slice(i), right.slice(j));
235242
};
236243

237-
const mid = Math.floor(arr.length / 2);
244+
const mid = Math.floor(arr.length / 2); // be aware that it's not `arr.length - 1`
238245
return merge(mergeSort(arr.slice(0, mid)), mergeSort(arr.slice(mid)));
239246
}
240247

@@ -280,4 +287,4 @@ export function heapSort(array) {
280287
}
281288

282289
return arr;
283-
}
290+
}

0 commit comments

Comments
 (0)