📄 快速排序递归算法.cpp
字号:
//快速排序递归算法
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define N 10
int QKPass(int r[], int low, int high)
{
int k0,Pkey=r[low];
r[0]=r[low];
while(low<high)
{
while(low<high && r[high]>=Pkey) --high;
k0=r[low];
r[low]=r[high];
r[high]=k0;
while(low<high && r[low]<=Pkey) ++low;
k0=r[low];
r[low]=r[high];
r[high]=k0;
} //while
r[low]=r[0];
return low; //返回枢轴所在位置
} //QKPass
void QKSort(int r[], int s, int t)
{
//对记录序列L.r[s..t]进行快速排序
if (s<t)
{
//长度大于1
int pos=QKPass(r, s, t);
//对 L.r[s..t] 进行一次划分
QKSort(r, s, pos-1);
//对低子序列递归排序, pos是枢轴位置
QKSort(r, pos+1, t); //对高子序列递归排序
} //if
} //QKSort
void main()
{
srand(time(0));
int i,r[N+1];
for(i=1; i<=N; i++) r[i]=rand();
QKSort(r, 1, N);
for(i=1; i<=N; i++) cout<<r[i]<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -