-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquick.html
65 lines (57 loc) · 1.38 KB
/
quick.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
// 先来一版好懂的 但是空间复杂度差一些
let arr = [8,11,4,3,2,1,9,6,0]
// console.log(quickSort(arr))
// 原地快排
function quickSort1(arr, low=0,high=arr.length-1){
//
//
// | |
// 6 0 ,4,3,2,1,8,9,11
// | |
if(low>=high){
return
}
let left = low
let right = high
let flag = arr[left]
while(left<right){
// 从右边尝试找比flag小的, 比flag大,right坐移
if(left<right && flag<=arr[right]){
right --
}
arr[left] = arr[right]
if(left<right && flag>=arr[left]){
left ++
}
arr[right] = arr[left]
}
arr[left] = flag
// quickSort1(arr,low,left-1)
// quickSort1(arr,left+1,high)
return arr
}
console.log([8,11,4,3,2,1,9,6,0])
console.log(quickSort1(arr))
// 为啥要取第一个
// 其实随机的 只是为了发方便
// 快速排序的复杂度是多少 (时间)
// n*log n
// 快速排序啥时候复杂度最差,如果一个数字已经排好序的
// 复杂度是O(n^2) 取第1个的不好
// 1. 随机取索引
// 2. 快排之前,先做一下乱序
// [3,2,4,6,2,1]
// [4,2,3,6,2,1]
</script>
</body>
</html>