2023-02-16 03:39:01 +08:00
|
|
|
|
# 快速排序
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
「快速排序 Quick Sort」是一种基于分治思想的排序算法,运行高效,应用广泛。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
快速排序的核心操作是「哨兵划分」,其目标是:选择数组中的某个元素作为“基准数”,将所有小于基准数的元素移到其左侧,而大于基准数的元素移到其右侧。具体来说,哨兵划分的流程为:
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
1. 选取数组最左端元素作为基准数,初始化两个指针 `i` 和 `j` 分别指向数组的两端;
|
|
|
|
|
2. 设置一个循环,在每轮中使用 `i`(`j`)分别寻找第一个比基准数大(小)的元素,然后交换这两个元素;
|
|
|
|
|
3. 循环执行步骤 `2.` ,直到 `i` 和 `j` 相遇时停止,最后将基准数交换至两个子数组的分界线;
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
哨兵划分完成后,原数组被划分成三部分:左子数组、基准数、右子数组,且满足“左子数组任意元素 $\leq$ 基准数 $\leq$ 右子数组任意元素”。因此,我们接下来只需对这两个子数组进行排序。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<1>"
|
2023-02-26 19:22:46 +08:00
|
|
|
|
![哨兵划分步骤](quick_sort.assets/pivot_division_step1.png)
|
2023-01-15 23:32:58 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<2>"
|
2022-11-23 03:56:25 +08:00
|
|
|
|
![pivot_division_step2](quick_sort.assets/pivot_division_step2.png)
|
2023-01-15 23:32:58 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<3>"
|
2022-11-23 03:56:25 +08:00
|
|
|
|
![pivot_division_step3](quick_sort.assets/pivot_division_step3.png)
|
2023-01-15 23:32:58 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<4>"
|
2022-11-23 03:56:25 +08:00
|
|
|
|
![pivot_division_step4](quick_sort.assets/pivot_division_step4.png)
|
2023-01-15 23:32:58 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<5>"
|
2022-11-23 03:56:25 +08:00
|
|
|
|
![pivot_division_step5](quick_sort.assets/pivot_division_step5.png)
|
2023-01-15 23:32:58 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<6>"
|
2022-11-23 03:56:25 +08:00
|
|
|
|
![pivot_division_step6](quick_sort.assets/pivot_division_step6.png)
|
2023-01-15 23:32:58 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<7>"
|
2022-11-23 03:56:25 +08:00
|
|
|
|
![pivot_division_step7](quick_sort.assets/pivot_division_step7.png)
|
2023-01-15 23:32:58 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<8>"
|
2022-11-23 03:56:25 +08:00
|
|
|
|
![pivot_division_step8](quick_sort.assets/pivot_division_step8.png)
|
2023-01-15 23:32:58 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<9>"
|
2022-11-23 03:56:25 +08:00
|
|
|
|
![pivot_division_step9](quick_sort.assets/pivot_division_step9.png)
|
|
|
|
|
|
2023-02-27 19:08:32 +08:00
|
|
|
|
!!! note "快速排序的分治思想"
|
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
哨兵划分的实质是将一个较长数组的排序问题简化为两个较短数组的排序问题。
|
2023-02-27 19:08:32 +08:00
|
|
|
|
|
2022-11-23 03:56:25 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
2023-02-07 04:43:52 +08:00
|
|
|
|
```java title="quick_sort.java"
|
|
|
|
|
[class]{QuickSort}-[func]{swap}
|
|
|
|
|
|
|
|
|
|
[class]{QuickSort}-[func]{partition}
|
2022-11-23 03:56:25 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="quick_sort.cpp"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{QuickSort}-[func]{swap}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{QuickSort}-[func]{partition}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-26 01:40:49 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="quick_sort.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{QuickSort}-[func]{partition}
|
2022-11-26 01:40:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="quick_sort.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{quickSort}-[func]{partition}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 19:45:06 +08:00
|
|
|
|
```javascript title="quick_sort.js"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{QuickSort}-[func]{swap}
|
|
|
|
|
|
|
|
|
|
[class]{QuickSort}-[func]{partition}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="quick_sort.ts"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{QuickSort}-[func]{swap}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{QuickSort}-[func]{partition}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="quick_sort.c"
|
2023-04-18 20:21:31 +08:00
|
|
|
|
[class]{}-[func]{partition}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="quick_sort.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{QuickSort}-[func]{swap}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{QuickSort}-[func]{partition}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="quick_sort.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{swap}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{partition}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="quick_sort.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{QuickSort}-[func]{swap}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{QuickSort}-[func]{partition}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 算法流程
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
1. 首先,对原数组执行一次「哨兵划分」,得到待排序的左子数组和右子数组;
|
|
|
|
|
2. 然后,对左子数组和右子数组分别递归执行「哨兵划分」;
|
|
|
|
|
3. 持续递归,直至子数组长度为 1 时终止,从而完成整个数组的排序;
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![快速排序流程](quick_sort.assets/quick_sort_overview.png)
|
2022-11-23 15:50:59 +08:00
|
|
|
|
|
2022-11-23 03:56:25 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
2022-11-23 21:39:39 +08:00
|
|
|
|
```java title="quick_sort.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{QuickSort}-[func]{quickSort}
|
2022-11-23 03:56:25 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="quick_sort.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{QuickSort}-[func]{quickSort}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-26 01:40:49 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="quick_sort.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{QuickSort}-[func]{quick_sort}
|
2022-11-26 01:40:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="quick_sort.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{quickSort}-[func]{quickSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="quick_sort.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{QuickSort}-[func]{quickSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="quick_sort.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{QuickSort}-[func]{quickSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="quick_sort.c"
|
2023-04-18 20:21:31 +08:00
|
|
|
|
[class]{}-[func]{quickSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="quick_sort.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{QuickSort}-[func]{quickSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="quick_sort.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{quickSort}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="quick_sort.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{QuickSort}-[func]{quickSort}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 算法特性
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
**时间复杂度 $O(n \log n)$** :在平均情况下,哨兵划分的递归层数为 $\log n$ ,每层中的总循环数为 $n$ ,总体使用 $O(n \log n)$ 时间。
|
2023-03-26 04:47:05 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
在最差情况下,每轮哨兵划分操作都将长度为 $n$ 的数组划分为长度为 $0$ 和 $n - 1$ 的两个子数组,此时递归层数达到 $n$ 层,每层中的循环数为 $n$ ,总体使用 $O(n^2)$ 时间;因此快速排序是“自适应排序”。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
**空间复杂度 $O(n)$** :在输入数组完全倒序的情况下,达到最差递归深度 $n$ 。由于未使用辅助数组,因此算法是“原地排序”。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
在哨兵划分的最后一步,基准数可能会被交换至相等元素的右侧,因此是“非稳定排序”。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 快排为什么快?
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
从名称上就能看出,快速排序在效率方面应该具有一定的优势。尽管快速排序的平均时间复杂度与「归并排序」和「堆排序」相同,但通常快速排序的效率更高,原因如下:
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
- **出现最差情况的概率很低**:虽然快速排序的最差时间复杂度为 $O(n^2)$ ,没有归并排序稳定,但在绝大多数情况下,快速排序能在 $O(n \log n)$ 的时间复杂度下运行。
|
|
|
|
|
- **缓存使用效率高**:在执行哨兵划分操作时,系统可将整个子数组加载到缓存,因此访问元素的效率较高。而像「堆排序」这类算法需要跳跃式访问元素,从而缺乏这一特性。
|
|
|
|
|
- **复杂度的常数系数低**:在上述三种算法中,快速排序的比较、赋值、交换等操作的总数量最少。这与「插入排序」比「冒泡排序」更快的原因类似。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 基准数优化
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
**快速排序在某些输入下的时间效率可能降低**。举一个极端例子,假设输入数组是完全倒序的,由于我们选择最左端元素作为基准数,那么在哨兵划分完成后,基准数被交换至数组最右端,导致左子数组长度为 $n - 1$ 、右子数组长度为 $0$ 。如此递归下去,每轮哨兵划分后的右子数组长度都为 $0$ ,分治策略失效,快速排序退化为「冒泡排序」。
|
|
|
|
|
|
|
|
|
|
为了尽量避免这种情况发生,**我们可以优化哨兵划分中的基准数的选取策略**。例如,我们可以随机选取一个元素作为基准数。然而,如果运气不佳,每次都选到不理想的基准数,效率仍然不尽如人意。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
需要注意的是,编程语言通常生成的是“伪随机数”。如果我们针对伪随机数序列构建一个特定的测试样例,那么快速排序的效率仍然可能劣化。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
为了进一步改进,我们可以在数组中选取三个候选元素(通常为数组的首、尾、中点元素),**并将这三个候选元素的中位数作为基准数**。这样一来,基准数“既不太小也不太大”的概率将大幅提升。当然,我们还可以选取更多候选元素,以进一步提高算法的稳健性。采用这种方法后,时间复杂度劣化至 $O(n^2)$ 的概率大大降低。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
2022-11-23 21:39:39 +08:00
|
|
|
|
```java title="quick_sort.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{medianThree}
|
|
|
|
|
|
|
|
|
|
[class]{QuickSortMedian}-[func]{partition}
|
2022-11-23 03:56:25 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="quick_sort.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{medianThree}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{partition}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-26 01:40:49 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="quick_sort.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{median_three}
|
2022-11-26 01:40:49 +08:00
|
|
|
|
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{partition}
|
2022-11-26 01:40:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="quick_sort.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{quickSortMedian}-[func]{medianThree}
|
|
|
|
|
|
|
|
|
|
[class]{quickSortMedian}-[func]{partition}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="quick_sort.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{medianThree}
|
|
|
|
|
|
|
|
|
|
[class]{QuickSortMedian}-[func]{partition}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="quick_sort.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{medianThree}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{partition}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="quick_sort.c"
|
2023-04-18 20:21:31 +08:00
|
|
|
|
[class]{}-[func]{medianThree}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-04-18 20:21:31 +08:00
|
|
|
|
[class]{}-[func]{partitionMedian}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="quick_sort.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{medianThree}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{partition}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="quick_sort.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{medianThree}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{partitionMedian}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="quick_sort.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{medianThree}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{QuickSortMedian}-[func]{partition}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 尾递归优化
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
**在某些输入下,快速排序可能占用空间较多**。以完全倒序的输入数组为例,由于每轮哨兵划分后右子数组长度为 $0$ ,递归树的高度会达到 $n - 1$ ,此时需要占用 $O(n)$ 大小的栈帧空间。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
为了防止栈帧空间的累积,我们可以在每轮哨兵排序完成后,比较两个子数组的长度,**仅对较短的子数组进行递归**。由于较短子数组的长度不会超过 $\frac{n}{2}$ ,因此这种方法能确保递归深度不超过 $\log n$ ,从而将最差空间复杂度优化至 $O(\log n)$ 。
|
2022-11-23 03:56:25 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
2022-11-23 21:39:39 +08:00
|
|
|
|
```java title="quick_sort.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{QuickSortTailCall}-[func]{quickSort}
|
2022-12-01 18:28:57 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="quick_sort.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{QuickSortTailCall}-[func]{quickSort}
|
2022-11-23 03:56:25 +08:00
|
|
|
|
```
|
2022-11-26 01:40:49 +08:00
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="quick_sort.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{QuickSortTailCall}-[func]{quick_sort}
|
2022-11-26 01:40:49 +08:00
|
|
|
|
```
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="quick_sort.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{quickSortTailCall}-[func]{quickSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="quick_sort.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{QuickSortTailCall}-[func]{quickSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="quick_sort.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{QuickSortTailCall}-[func]{quickSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="quick_sort.c"
|
2023-04-18 20:21:31 +08:00
|
|
|
|
[class]{}-[func]{quickSortTailCall}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="quick_sort.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{QuickSortTailCall}-[func]{quickSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="quick_sort.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{quickSortTailCall}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="quick_sort.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{QuickSortTailCall}-[func]{quickSort}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
2023-03-26 22:02:37 +08:00
|
|
|
|
|
|
|
|
|
!!! question "哨兵划分中“从右往左查找”与“从左往右查找”的顺序可以交换吗?"
|
|
|
|
|
|
|
|
|
|
不行,当我们以最左端元素为基准数时,必须先“从右往左查找”再“从左往右查找”。这个结论有些反直觉,我们来剖析一下原因。
|
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
哨兵划分 `partition()` 的最后一步是交换 `nums[left]` 和 `nums[i]` 。完成交换后,基准数左边的元素都 `<=` 基准数,**这就要求最后一步交换前 `nums[left] >= nums[i]` 必须成立**。假设我们先“从左往右查找”,那么如果找不到比基准数更小的元素,**则会在 `i == j` 时跳出循环,此时可能 `nums[j] == nums[i] > nums[left]`**。也就是说,此时最后一步交换操作会把一个比基准数更大的元素交换至数组最左端,导致哨兵划分失败。
|
2023-03-26 22:02:37 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
举个例子,给定数组 `[0, 0, 0, 0, 1]` ,如果先“从左向右查找”,哨兵划分后数组为 `[1, 0, 0, 0, 0]` ,这个结果是不正确的。
|
2023-03-26 22:02:37 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
再深入思考一下,如果我们选择 `nums[right]` 为基准数,那么正好反过来,必须先“从左往右查找”。
|