快速排序法,没有输出
没有输出,好奇怪,麻烦鱼油们看一下吧#include<stdio.h>
void supersort(int a[],int left,int right)
{
int i = left,j = right,middle = (i+j)/2;
int flag = a,temp;
while(i<=j)
{
while(a <= flag)
{
i++;
}
while(a >= flag)
{
j++;
}
if(i<=j)
{
temp = a;
a = a;
a = temp;
i++;
j--;
}
}
if(left < j)
supersort(a,left,j);
if(right > i)
supersort(a,i,right);
}
int main()
{
int a={91,95,102,100,144,155,454,231,45,1},length,i=0;
length = 10;
supersort(a,0,length-1);
for(;i<10;i++)
printf("%4d",a);
return 0;
} 发现了j++不对,应该是j--,改了一下,但是还是不对,好像是堆栈溢出
#include<stdio.h>
void supersort(int a[],int left,int right)
{
int i = left,j = right,middle = (i+j)/2;
int flag = a,temp;
while(i<=j)
{
while(a <= flag)
{
i++;
}
while(a >= flag)
{
j--;
}
if(i<=j)
{
temp = a;
a = a;
a = temp;
i++;
j--;
}
}
if(left < j)
supersort(a,left,j);
if(right > i)
supersort(a,i,right);
}
int main()
{
int a={91,95,102,100,144,155,454,231,45,1},length,i=0;
length = 10;
supersort(a,0,length-1);
for(;i<10;i++)
printf("%4d",a);
return 0;
} 本帖最后由 jhanker 于 2020-7-3 17:14 编辑
#include<stdio.h>
void supersort(int a[],int left,int right)
{
if(left< right){
int i = left,j= left,middle = (i+j)/2;
int flag = a,temp;
temp=a;
a=a;
a=temp;
for( ; j < right ; ++j){
if(a <= flag){
temp=a;
a=a;
a=temp;
i++;
}
}
temp=a;
a=a;
a=temp;
supersort(a,left,i-1);
supersort(a,i+1,right);
}
}
int main()
{
int a={91,95,102,100,144,155,454,231,45,1},length,i=0;
length = 10;
supersort(a,0,length-1);
for(;i<10;i++)
printf("%4d",a);
return 0;
}
页:
[1]