This repository was archived by the owner on Mar 22, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (64 loc) · 1.9 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function quickSort(arr) {
return tailRecursiveQuickSort(arr, 0, arr.length - 1);
}
function tailRecursiveQuickSort(arr, start, end) {
while (start < end) {
const pivotIndex = partition(arr, start, end);
if (pivotIndex - start < end - pivotIndex) {
tailRecursiveQuickSort(arr, start, pivotIndex - 1);
start = pivotIndex + 1;
} else {
tailRecursiveQuickSort(arr, pivotIndex + 1, end);
end = pivotIndex - 1;
}
}
return arr;
}
function partition(arr, start, end) {
const pivot = arr[end];
let i = start - 1;
for (let j = start; j < end; j++) {
if (arr[j] <= pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
[arr[i + 1], arr[end]] = [arr[end], arr[i + 1]];
return i + 1;
}
function mergeSort(arr) {
if (arr.length <= 1) {
return arr;
}
const middle = Math.floor(arr.length / 2);
const left = arr.slice(0, middle);
const right = arr.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right) {
let result = [];
let leftIndex = 0;
let rightIndex = 0;
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
result.push(left[leftIndex]);
leftIndex++;
} else {
result.push(right[rightIndex]);
rightIndex++;
}
}
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}
export default function Sort(arr) {
// Determine the best sorting method based on array characteristics
if (arr.length > 899) {
// Use mergesort for larger arrays
// console.log("Using mergesort");
return mergeSort(arr);
} else {
// Use quicksort for smaller arrays
// console.log("Using quicksort");
return quickSort(arr);
}
}