|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
void quick_sort(int *,int,int);
void quick_sort(int array[],int left,int right)
{
int i = left;
int j = right;
int temp;
int pivot = array[(right - left) / 2];
while (i <= j)
{
while (array[i] < pivot)
{
i++;
}
while (array[j] > pivot)
{
j--;
}
if (i <= j)
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
i++;
j--;
}
}
if (j > left)
{
quick_sort(array,left,j);
}
if (i < right)
{
quick_sort(array,i,right);
}
}
int main(void)
{
int array[] = {45,69,88,33,55,22,16,77,987,44,61};
int i,length;
length = sizeof(array) / sizeof(array[0]);
quick_sort(array,0,length-1);
printf("排序的结果是:\n");
for (i = 0;i < length;i++)
{
printf("%d\n",array[i]);
}
putchar('\n');
return 0;
}
哪个大哥能看看这个哪里出问题了,为什么一运行就停止工作
#include <stdio.h>
void quick_sort(int *,int,int);
void quick_sort(int array[],int left,int right)
{
int i = left;
int j = right;
int temp;
int pivot = array[(right + left) / 2]; // 这里求得难道不该是中位数吗?
while (i <= j)
{
while (array[i] < pivot)
{
i++;
}
while (array[j] > pivot)
{
j--;
}
if (i <= j)
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
i++;
j--;
}
}
if (j > left)
{
quick_sort(array,left,j);
}
if (i < right)
{
quick_sort(array,i,right);
}
}
int main(void)
{
int array[] = {45,69,88,33,55,22,16,77,987,44,61};
int i,length;
length = sizeof(array) / sizeof(array[0]);
quick_sort(array,0,length-1);
printf("排序的结果是:\n");
for (i = 0;i < length;i++)
{
printf("%d\n",array[i]);
}
putchar('\n');
return 0;
}
|
|