c语言快速排序求助
#include <stdio.h>void quicksort(int a[] , int left ,int right);
void quicksort(int a[] , int left ,int right)
{
int temp;
int point;
int i=left , j = right;
point = a[(left +right)/2];
while (i <= j)
{
while (a < point)
{
i++;
}
while (a > point)
{
j--;
}
if (i <= j)
{
temp = a;
a = a;
a = temp;
j--; // 这里为什么要 j-- 和 i++ 呢
i++;
}
}
if (left < j)
{
quicksort(a, left, j);
}
if (i < right)
{
quicksort(a , i , right);
}
}
int main()
{
int a[] = {1 , 2 , 7 , 9 ,8 ,6 ,0 ,5 , 4 ,3};
int i, length;
length = sizeof(a)/sizeof(a) ;
quicksort(a, 0 , length);
printf("快速排序的结果是:");
for (i=0; i<length ; i++)
{
printf("%d ",a);
}
return 0;
} i是从左往右遍历,j则相反
页:
[1]