1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-02-03 18:43:20 +08:00
Files
408CSFamily/code/ds/QuickSort.cpp
最近在学桌球 6a1567cd2e feat: 更新文档
2023-08-30 17:04:20 +08:00

46 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* @Description: 快速排序【伪代码】
* @Version: Beta1.0
* @Author: 【B站&公众号】Rong姐姐好可爱
* @Date: 2020-03-23 08:23:20
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
* @LastEditTime: 2021-04-08 21:51:28
*/
void QuickSort(ElemType A[] , int low , int high){
// low > high 表角标越界low=high 子表只有一个元素,不需要进行快排,已经有序
if(low<high){
// 获取pivot基准将当前待排序表分成左右两个子表
int pivotKey = Partition(A,low,high)
// 对左边序列进行快排
QuickSort(A,low,pivotKey-1)
// 对右边序列进行快排
QuickSort(A,pivotKey+1,high)
}
return A
}
int Partition(ElemType A ,int low , int high){
ElemType pivot=A[low];
while(low<high){
while(low<high && A[high]>=pivot) --high
A[low]=A[high] // 比pivot小的都移到左表 注意--high 从后往前遍历
while(low<high && A[low]<=pivot ) ++low
A[high]=A[low] // 比pivot大的都移到右表注意++low 从前往后遍历
}
// 此时low==high||low>high 跳出循环后即找到能将当前表一分为二的pivotKey值
A[low]=pivot
// 基准元素pivot对应最终的位置角标
return low
}