-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.js
68 lines (57 loc) · 1.48 KB
/
mergeSort.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
// Description
// Given an integer array, sort it in ascending order.Use quick sort, merge sort, heap sort or any O(nlogn) algorithm.
// Example
// Given[3, 2, 1, 4, 5], return [1, 2, 3, 4, 5].
var sortIntegers2 = (A) => {
if (A.length <= 1) return A // incase of []
let mid = Math.floor(A.length / 2)
let left = A.slice(0, mid)
let right = A.slice(mid)
return merge(sortIntegers2(left), sortIntegers2(right))
}
var merge = (arrLeft, arrRight) => {
let i = 0
let j = 0
let temp = []
while (i < arrLeft.length && j < arrRight.length) {
if (arrLeft[i] < arrRight[j]) {
temp.push(arrLeft[i])
i++
} else {
temp.push(arrRight[j])
j++
}
}
return temp.concat(arrLeft.slice(i)).concat(arrRight.slice(j))
}
console.log(sortIntegers2([3, 2, 1, 4, 5]))
// for lintcode
var sortIntegers2 = function (A) {
if (A.length <= 1) {
return A
}
let mid = Math.floor(A.length / 2)
let left = A.slice(0, mid)
let right = A.slice(mid)
let temp = []
return merge(sortIntegers2(left), sortIntegers2(right), temp, A)
}
var merge = (arrLeft, arrRight, temp, A) => {
let i = 0
let j = 0
while (i < arrLeft.length && j < arrRight.length) {
if (arrLeft[i] < arrRight[j]) {
temp.push(arrLeft[i])
i++
} else {
temp.push(arrRight[j])
j++
}
}
temp = temp.concat(arrLeft.slice(i)).concat(arrRight.slice(j))
// for lintcode
for (let idx = 0; idx < A.length; idx++) {
A[idx] = temp[idx]
}
return temp
}