-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.js
More file actions
61 lines (59 loc) · 1.24 KB
/
mergeSort.js
File metadata and controls
61 lines (59 loc) · 1.24 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
54
55
56
57
58
59
60
61
let {genDisorderList} = require("./tools")
/***
* 合并排序
* @param array
* @returns {*}
*/
function sort(array) {
if (!(array[0] instanceof Array)) {
let a = []
for (let i = 0; i < array.length - 1; i += 2) {
let tmp = []
if (array[i] < array[i + 1]) {
tmp.push(array[i])
tmp.push(array[i + 1])
} else {
tmp.push(array[i + 1])
tmp.push(array[i])
}
a.push(tmp)
}
array = sort(a)
} else {
if (array.length === 1) {
return array[0]
}
let a = []
for (let t = 0; t < array.length - 1; t += 2) {
let tmp = []
for (let i = 0, j = 0; ;) {
if (array[t][i] < array[t + 1][j]) {
tmp.push(array[t][i])
if (i === array[t].length - 1) {
tmp.push(...array[t + 1].slice(j))
break
}
i++
} else {
tmp.push(array[t + 1][j])
if (j === array[t + 1].length - 1) {
tmp.push(...array[t].slice(i))
break
}
j++
}
}
a.push(tmp)
}
// 如果数组是奇数项,则最后多出的一项追加到尾部
if (array.length % 2 !== 0) {
a.push(array[array.length - 1])
}
array = sort(a)
}
return array
}
let origin = genDisorderList(1000000)
let begTime = Date.now()
let result = sort(origin)
console.log(`耗时:${Date.now() - begTime}`)