-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay63.js
More file actions
40 lines (33 loc) · 1.17 KB
/
Day63.js
File metadata and controls
40 lines (33 loc) · 1.17 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
function mergeSort(arr) {
// Base case: arrays with fewer than 2 elements are already sorted
if (arr.length <= 1) {
return arr;
}
// Split the array into two halves
const mid = Math.floor(arr.length / 2);
const left = arr.slice(0, mid);
const right = arr.slice(mid);
// Recursively sort each half
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right) {
const result = [];
let leftIndex = 0;
let rightIndex = 0;
// Compare the elements of both halves and merge them in sorted order
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
result.push(left[leftIndex]);
leftIndex++;
} else {
result.push(right[rightIndex]);
rightIndex++;
}
}
// Concatenate any remaining elements (one of the halves may have elements left)
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}
// Test the function
const array = [38, 27, 43, 3, 9, 82, 10];
const sortedArray = mergeSort(array);
console.log(sortedArray); // Output: [3, 9, 10, 27, 38, 43, 82]