-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-sort.js
More file actions
53 lines (44 loc) · 1.36 KB
/
merge-sort.js
File metadata and controls
53 lines (44 loc) · 1.36 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
41
42
43
44
45
46
47
48
49
50
51
52
53
// Function to implement Merge Sort
function mergeSort(arr) {
// Base case: if the array is empty or has one element, it's already sorted
if (arr.length <= 1) {
return arr;
}
// Split the array into two halves
const middle = Math.floor(arr.length / 2);
const leftHalf = arr.slice(0, middle);
const rightHalf = arr.slice(middle);
// Recursively sort both halves
const sortedLeft = mergeSort(leftHalf);
const sortedRight = mergeSort(rightHalf);
// Merge the sorted halves
return merge(sortedLeft, sortedRight);
}
// Helper function to merge two sorted arrays
function merge(leftArr, rightArr) {
const result = [];
let leftIndex = 0;
let rightIndex = 0;
// Compare elements from both arrays and add the smaller one to the result
while (leftIndex < leftArr.length && rightIndex < rightArr.length) {
if (leftArr[leftIndex] < rightArr[rightIndex]) {
result.push(leftArr[leftIndex]);
leftIndex++;
} else {
result.push(rightArr[rightIndex]);
rightIndex++;
}
}
// Add any remaining elements from the left array
while (leftIndex < leftArr.length) {
result.push(leftArr[leftIndex]);
leftIndex++;
}
// Add any remaining elements from the right array
while (rightIndex < rightArr.length) {
result.push(rightArr[rightIndex]);
rightIndex++;
}
return result;
}
module.exports = mergeSort;