-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.js
More file actions
41 lines (37 loc) · 975 Bytes
/
quickSort.js
File metadata and controls
41 lines (37 loc) · 975 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
37
38
39
40
41
const {genDisorderList} = require("./tools")
function sort(array, beg = 0, end = array.length - 1) {
if (beg < end) {
let idx = partition(array, beg, end)
sort(array, beg, idx - 1)
sort(array, idx + 1, end)
}
}
function partition(array, beg, end) {
// 确定分隔元素
// beg - splIdx:比分隔元素小的元素
// splIdx - i:比分隔元素大的元素
let split = array[end]
let smlIdx = beg
for (let bigIdx = beg; bigIdx < end; bigIdx++) {
if (array[bigIdx] < split) {
if (bigIdx !== 0 && array[bigIdx - 1] > split) {
swap(array, smlIdx, bigIdx)
}
smlIdx++
}
}
if (smlIdx !== end) {
swap(array, smlIdx, end)
}
return smlIdx
}
function swap(array, index1, index2) {
array[index1] += array[index2]
array[index2] = array[index1] - array[index2]
array[index1] -= array[index2]
}
let origin = genDisorderList(1000000)
// console.log(origin)
let begTime = Date.now()
sort(origin)
console.log(`耗时:${Date.now() - begTime}`)