📄 高级排序:快速排序.cpp
字号:
#include<iostream.h>
int Partition ( int R[], int low,int high) {
int i=low;
int j=high;
int pivotkey = R[low]; // 枢轴记录关键字
while (low<high) {
while(low<high&& R[high]>=pivotkey)
--high;
R[low++] = R[high]; // 将比枢轴记录小的记录移到低端
while (low<high && R[low]<=pivotkey)
++low;
if(low>high)R[--low]=pivotkey;// 对在low>high的情况做特殊处理
else R[high--] = R[low]; // 将比枢轴记录大的记录移到高端
} //while
R[low]=pivotkey; // 枢轴记录移到正确位置
return low; // 返回枢轴位置
} // Partition
void QSort (int R[], int s, int t ) {
if (s<t) {
int pivotloc = Partition(R, s, t);
QSort(R, s, pivotloc-1); // 对低子序列递归排序
QSort(R, pivotloc+1, t); // 对高子序列递归排序
} // if
} // Qsort
void main()
{
int x[20];
int i;
cout<<"input 20 number:\n";
for(i=0;i<20;i++)cin>>x[i];
QSort (x,0,19);
cout<<"the result is:\n";
for(i=0;i<20;i++)cout<<x[i]<<" ";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -