-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathquick-sort.module.js
43 lines (37 loc) · 993 Bytes
/
quick-sort.module.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
(function (exports) {
let alist, swap
function quickSortHelper (left, right) {
if (right - left <= 0) {
return
} else {
const pivot = alist[right]
const splitPoint = partition(left, right, pivot)
quickSortHelper(left, splitPoint - 1)
quickSortHelper(splitPoint + 1, right)
}
}
function partition (left, right, pivot) {
let leftmark = left - 1
let rightmark = right
while (true) {
while (alist[++leftmark] < pivot) {}
while (rightmark > 0 && alist[--rightmark] > pivot) {}
if (rightmark <= leftmark) {
break
} else {
swap(leftmark, rightmark)
}
}
swap(leftmark, right)
return leftmark
}
const quickObject = {
sort (...args) {
alist = args[0]
swap = args[1]
quickSortHelper(0, alist.length - 1)
return alist
}
}
Object.assign(exports, {quickSort: quickObject})
}((typeof module.exports !== undefined) ? module.exports : window))