#include<stdio.h>
#include<string.h>
struct info
{
char name[100];
int score;
};
struct info temp;
struct info x[10];
int Qksort(struct info arr[],int low,int high)
{
int max = high;
int min = low;
int flag = 0;
temp = arr[low];
while(low<high)
{
while(arr[high].score > temp.score )
high--;
arr[low] = arr[high];
while (arr[low].score < temp.score)
low++;
arr[high] = arr[low];
flag++;
}
if(flag != 0)
{
arr[low] = arr[high] = temp;
Qksort(arr,min,high);
Qksort(arr,high+1,max);
}
}
int main()
{
int m = 10;
int i,j;
for (i=0; i<m; i++)
{
printf("输入第%d个学生的信息:",i+1);
scanf("%s %d",x[i].name,&x[i].score);
}
Qksort(x,0,9);
printf("使用快速排序对上面数据按成绩非递减排列:\n");
for (i=0; i<m; i++)
{
printf("%s %d\n",x[i].name,x[i].score);
}
return 0;
}
陷入死循环了,arr[high].score > temp.score里应该加上low<high的判断,要不然从头找到尾了
你的输入是什么? 它需要 10 个元素。
程序显示了 “使用快速排序对上面数据按成绩非递减排列” 了吗?
10个姓名和对应的成绩 没有显示