一个快速排序算法

来源:百度知道 编辑:UC知道 时间:2024/06/28 09:40:51
快速排序算法
#include<iostream>
using namespace std;
int Po(int,int,int);
void Q(int a[],int p,int r)
{
int q;

if(p<r)q=Po(a,p,r);
Q(a,p,q-1);
Q(a,p+1,r);
};

int Po(int a[],int p,int r)
{
int x=a[r],i=p-1,y;
for(int j=p;j<=r-1;j++)
{
if(a[j]<=x)
{
i=i+1;y=a[i];a[i+1]=a[r];a[r]=y;}
}
return i+1;
};

void main()
{
int a[10]={1,2,3,4,5,6,7,8,9,0};
Q(a,1,10);
for(int i=0;i<10;i++)cout<<a[i]<<endl;
}

报告第8行 就是if(p<r)q=Po(a,p,r); 这里Po里a错了
可是我发现不了
哪位高人能指点一下 谢谢。

解答同上!
或者把
int Po(int a[],int p,int r)
{
int x=a[r],i=p-1,y;
for(int j=p;j<=r-1;j++)
{
if(a[j]<=x)
{
i=i+1;y=a[i];a[i+1]=a[r];a[r]=y;}
}
return i+1;
};
放到
void Q(int a[],int p,int r)
{
int q;

if(p<r)q=Po(a,p,r);
Q(a,p,q-1);
Q(a,p+1,r);
};
上面,这样“int Po(int,int,int);” 就不用写了,多省事!

int Po(int ,int,int); // 第一个参数的类型不对,应该是int []