-
Notifications
You must be signed in to change notification settings - Fork 31
/
QuickSort.ts
66 lines (55 loc) · 1.55 KB
/
QuickSort.ts
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
// QucikSort 快速排序三路快排
// 不稳定
type Mutable<T> = { -readonly [P in keyof T]: T[P] }
function quickSort<T>(
arr: Mutable<ArrayLike<T>>,
compareFn: (a: T, b: T) => number,
start = 0,
end = arr.length
): void {
if (start < 0) start = 0
if (end > arr.length) end = arr.length
if (start >= end) return
partition(start, end - 1)
function partition(left: number, right: number): void {
if (left >= right) return
// 优化,随机取标定点,以解决近乎有序的列表
const randIndex = randint(left, right)
swap(left, randIndex)
const pivot = arr[left]
let leftPoint = left
let midPoint = left
let rightPoint = right
while (midPoint <= rightPoint) {
const compared = compareFn(arr[midPoint], pivot)
if (compared < 0) {
swap(leftPoint, midPoint)
leftPoint++
midPoint++
} else if (compared > 0) {
swap(midPoint, rightPoint)
rightPoint--
} else {
midPoint++
}
}
partition(left, leftPoint - 1)
partition(rightPoint + 1, right)
}
// [left,right]
function randint(left: number, right: number) {
const amplitude = right - left
return (((amplitude + 1) * Math.random()) | 0) + left
}
function swap(i: number, j: number): void {
const tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
}
export {}
if (require.main === module) {
const arr = [4, 3, 2, 5, 6, 7, 8, 3, 2, 4, 1]
quickSort(arr, (a, b) => a - b)
console.log(arr)
}