返回信息流#include <iostream>
using namespace std;
#define max 20
int A[max];
void quick_sort(int low,int high);
int partition(int i,int j);
void quick_sort(int low,int high)
{
int pivot;
if (low<high)
{
pivot=partition(low,high);
quick_sort(low,pivot-1);
quick_sort(pivot+1,high);
}
}
int partition(int i,int j)
{
int pivotpos=A[i];
while (i<j)
{
while (i<j&&A[j]>=pivotpos)
j--;
if (i<j)
A[i++]=A[j];
while (i<j&&A[i]<=pivotpos)
i++;
if (i<j)
A[j--]=A[i];
A[i]=pivotpos;
return i;
}
//pivotpos=A[i];
}
int main()
{
int A[max]={1,23,4,32,55,64,32,4,52,11,45,87,31,12,24,2,6,21,35,21};
cout<<"the data before sorting:"<<endl;
for (int i=0;i<max;++i)
{
cout<<setw(3)<<A[i];
}
cout<<endl;
quick_sort(0,max-1);
cout<<"the data after sorting:"<<endl;
for (i=0;i<max;++i)
{
cout<<setw(3)<<A[i];
}
cout<<endl;
return 0;
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #20104同步于 2009/3/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
帮忙看看,敲了个快速排序的例程,发现在主函数里没用调用排序
biti04314
2009/3/10镜像同步6 回复
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
quick_sort调用partition
【 在 biti04314 (APTX4869) 的大作中提到: 】
: #include <iostream>
: using namespace std;
: #define max 20
: ...................
单步调试没有进入子函数?什么意思?
数组A
一个是全局变量
一个是main中的局部变量~~
【 在 biti04314 (APTX4869) 的大作中提到: 】
: 可是没有实现排序,输出还是原来次序的。我用单步调试,也没有进入排序子函数
re, lz那两个函数操作的数组A是全局的A,最后打印出来的是main里面定义的A
【 在 guo 的大作中提到: 】
: 单步调试没有进入子函数?什么意思?
: 数组A
: 一个是全局变量
: ...................