-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathQuickSort.js
More file actions
39 lines (34 loc) · 800 Bytes
/
QuickSort.js
File metadata and controls
39 lines (34 loc) · 800 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
// 快速排序
// 不稳定
// 原地
const quickSort = (arr) => {
const n = arr.length
if (n <= 1) return;
_quickSort(arr, 0, n-1);
}
const _quickSort = (arr, left, right) => {
if (left >= right) return;
const pivot = _patition(arr, left, right);
_quickSort(arr, left, pivot - 1);
_quickSort(arr, pivot + 1, right);
}
const _patition = (arr, left, right) => {
const pivot = arr[right];
let i = left;
for (let j = left; j < right; ++j) {
if (arr[j] < pivot) {
_swap(arr, i, j);
++i;
}
}
_swap(arr, i, right);
return i;
}
const _swap = (arr, i, j) => {
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
const arr = [23, 3, 2, 12, 34, 23, 4, 32, 19, 39];
quickSort(arr);
console.log(arr);