1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-02-03 02:23:38 +08:00
Files
408CSFamily/code/ds/QuickSort.cpp
喜欢芝士的妹妹 6d81d1706a feat: 修改代码
2023-08-30 22:12:32 +08:00

35 lines
1011 B
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.
// 快速排序【伪代码】
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
}