-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.ts
38 lines (32 loc) · 808 Bytes
/
merge.ts
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
/**
* recursive split array and merge
*/
export const mergeSort = <T = number>(arr: T[]) => {
if (arr.length <= 1) {
return arr
}
const mid = Math.floor(arr.length / 2)
const left = mergeSort(arr.slice(0, mid))
const right = mergeSort(arr.slice(mid, arr.length))
function mergeSortFn(left: T[], right: T[]) {
const result: T[] = []
let l = 0,
r = 0,
min: T | undefined = undefined
// find min and push to result
while (l < left.length && r < right.length) {
min = undefined
if (left[l] <= right[r]) {
min = left[l]
l++
} else {
min = right[r]
r++
}
result.push(min)
}
// concat reminds
return [...result, ...left.slice(l), ...right.slice(r)]
}
return mergeSortFn(left, right)
}