-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.js
More file actions
36 lines (32 loc) · 852 Bytes
/
mergeSort.js
File metadata and controls
36 lines (32 loc) · 852 Bytes
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
const mergeSort = (arr, left, right) => {
const mid = Math.floor((left + right) / 2)
//base condition
if (left === right) {
return [arr[left]]
}
//좌, 우 파트로 분리하여 mergeSort
const leftPart = mergeSort(arr, left, mid)
const rightPart = mergeSort(arr, mid + 1, right)
//각 파트를 비교하며 분배
let i = 0
let j = 0
const totalArr = []
while (i < leftPart.length && j < rightPart.length) {
if (leftPart[i] < rightPart[j]) {
totalArr.push(leftPart[i])
i++
} else {
totalArr.push(rightPart[j])
j++
}
}
while (i < leftPart.length) {
totalArr.push(leftPart[i++])
}
while (j < rightPart.length) {
totalArr.push(rightPart[j++])
}
return totalArr
}
const arr = [40, 20, 50, 10, 30, 70, 60, 80, 100, 90]
console.log(mergeSort(arr, 0, arr.length - 1))