1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-04-02 02:00:44 +08:00

feat: 修复文档,新增算法代码

This commit is contained in:
妹妹下雨回不去
2023-03-02 17:39:42 +08:00
parent 7bd3072ee1
commit 8262d7ab42
58 changed files with 1725 additions and 1679 deletions

47
code/ds/QuickSort.cpp Normal file
View File

@@ -0,0 +1,47 @@
/*
* @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
}