forked from coder-hxl/x-crawl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.ts
48 lines (37 loc) · 867 Bytes
/
sort.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
function swap(arr: any[], i: number, j: number) {
const temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
export function quickSort<T extends any[]>(arr: T): T {
const n = arr.length
partition(0, n - 1)
function partition(left: number, right: number) {
if (left >= right) return
// 1.找基准元素
const pivot = arr[right]
// 2.定义双指针进行交换(左小右大)
let i = left
let j = right - 1
while (i <= j) {
while (arr[i] < pivot) {
i++
}
while (arr[j] > pivot) {
j--
}
if (i <= j) {
swap(arr, i, j)
i++
j--
}
}
// 3.将 pivot 放到正确位置
swap(arr, i, right)
// 4.左右划分区域
partition(left, i - 1)
partition(i + 1, right)
}
return arr
}
// console.log(quickSort([7, 3, 6, 4, 9, 2, 1, 5]))