您的位置:首页 > 其它

快速排序 --递归调用一个函数初始值被写死

2012-11-22 17:10 274 查看
这次的错误在:Pattition这个函数虽然不是递归函数但是它是被递归调用的,因此最小是a[low]

我开始单纯写这个函数的时候以为是调用第一个所以数组写成了a[0]

int Pattition(int a[],int low,int high)

{

int pivot=a[0];

#include<iostream>
using namespace std;
void QuickSort(int a[], int n);
void Qsort(int a[],int low,int high);
int Pattition(int a[],int low,int high);
void print(int a[],int low, int high);
int main()
{
int a[]= { 3,7,2,9,6,5,8,1,4};
cout<<"原排序为:"<<endl;
print(a,0,8);
QuickSort(a,9);
system("pause");
return 0;
}

void QuickSort(int a[], int n)
{
Qsort(a,0,n-1);
print(a,0,n-1);
}

void Qsort(int a[],int low,int high)
{
int pivotloc;
if(low < high)
{
pivotloc = Pattition(a,low,high);
cout<<"pivotloc:"<<pivotloc<<"--pivot"<<a[pivotloc]<<endl;
cout<<"Low:"<<endl;
print(a,low,pivotloc-1);
cout<<"high:"<<endl;
print(a,pivotloc+1,high);

cout<<"开始递归low部分:"<<endl;
Qsort(a,low,pivotloc-1);
cout<<"开始递归high部分:"<<endl;
Qsort(a,pivotloc+1,high);
}
}

int Pattition(int a[],int low,int high)
{
int pivot=a[low];
int temp_high,temp_low;

while(low<high)
{
while(low<high&&a[high]>=pivot)
{
--high;
}
temp_high=a[low]=a[high];
while(low<high&&a[low]<=pivot)
{
++low;
}
temp_low=a[high]=a[low];

}
a[low]=pivot;
return low;
}

void print(int a[],int low, int high)
{
for (int i = low; i <= high; ++i)
{
cout<<a[i]<<" ";
}
cout<<endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐