你的这个分治法的快速排序的程序是怎么修改的,我写了同样的代码问什么就是不行。C语言新手,请多多指教 5
#include<stdio.h>#include<stdlib.h>intt,n;voidswap(int&a,int&b){inttm;tm=a;a=b;b=tm;}...
#include<stdio.h>
#include<stdlib.h>
int t,n;
void swap(int &a,int &b)
{
int tm;
tm=a;a=b;b=tm;
}
int split(int a[],int low,int high)
{
int i=low+1,j=high,k;
int x=a[low];
while(i<=j)
{
while(a[i]<=x) i++;
while(a[j]>x) j--;
if(i<j)
swap(a[i],a[j]);
}
swap(a[low],a[j]);
printf("\n第%d次排序:",t++);
for(k=0;k<n;k++)
printf("%4d",a[k]);
printf("\n");
return j;
}
int quick_sort(int a[],int low,int high)
{
int k,i=0;
if(low<high)
{
k=split(a,low,high);
quick_sort(a,low,k-1);
quick_sort(a,k+1,high);
}
return 0;
}
void main()
{
int a[10]={3,5 ,7,8,1,2,4,0,9,6};
int i,k,n;
int low,high;
low=0;
high=n-1;
printf("The before a:");
for(i=0;i<10;i++)
printf("%d",a[i]);
quick_sort(a,low,high);
printf("\n");
printf("The finally a:");
for(k=0;k<10;k++)
printf("%d",a[k]);
printf("\n");
} 展开
#include<stdlib.h>
int t,n;
void swap(int &a,int &b)
{
int tm;
tm=a;a=b;b=tm;
}
int split(int a[],int low,int high)
{
int i=low+1,j=high,k;
int x=a[low];
while(i<=j)
{
while(a[i]<=x) i++;
while(a[j]>x) j--;
if(i<j)
swap(a[i],a[j]);
}
swap(a[low],a[j]);
printf("\n第%d次排序:",t++);
for(k=0;k<n;k++)
printf("%4d",a[k]);
printf("\n");
return j;
}
int quick_sort(int a[],int low,int high)
{
int k,i=0;
if(low<high)
{
k=split(a,low,high);
quick_sort(a,low,k-1);
quick_sort(a,k+1,high);
}
return 0;
}
void main()
{
int a[10]={3,5 ,7,8,1,2,4,0,9,6};
int i,k,n;
int low,high;
low=0;
high=n-1;
printf("The before a:");
for(i=0;i<10;i++)
printf("%d",a[i]);
quick_sort(a,low,high);
printf("\n");
printf("The finally a:");
for(k=0;k<10;k++)
printf("%d",a[k]);
printf("\n");
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询