bcd 19 97
bed 20 97
abc 20 99
之前老是用冒泡排序,感觉方便,写了篇快速排序之后,咱也得用起来,上AC代码
#include
#include
#include
struct student{
char name[101];
int age;
int grade;
};
int partition(struct student *A, int left, int right);
void quicksort(struct student *A, int begin, int end);
int main()
{
struct student students[1001];
int i, n;
while(scanf("%d",&n) != EOF)
{
//学生成绩赋值
for(i = 0; i < n; i ++)
{
scanf("%s%d%d",students[i].name, &students[i].age, &students[i].grade);
}
//快速排序
quicksort(students, 0, n-1);
//打印输出
for(i = 0; i < n; i ++)
{
printf("%s %d %d\n",students[i].name, students[i].age, students[i].grade);
}
}
return 0;
}
void quicksort(struct student *A, int begin, int end)
{
int pivot;
if(begin < end)
{
pivot = partition(A, begin, end);
quicksort(A, begin, pivot - 1);
quicksort(A, pivot + 1, end);
}
}
int partition(struct student *A, int left, int right)
{
struct student stand = A[left];
while(left < right)
{
while(left < right && (A[right].grade > stand.grade || (A[right].grade == stand.grade && strcmp(A[right].name,stand.name) > 0) || (A[right].grade == stand.grade && strcmp(A[right].name,stand.name) == 0 && A[right].age > stand.age ) ) )
{
right --;
}
if(left < right)
{
A[left ++] = A[right];
}
while(left < right && (A[left].grade < stand.grade || (A[left].grade == stand.grade && strcmp(A[left].name,stand.name) < 0) || (A[left].grade == stand.grade && strcmp(A[left].name,stand.name) == 0 && A[left].age < stand.age ) ) )
{
left ++;
}
if(left < right)
{
A[right --] = A[left];
}
}
A[left] = stand;
return left;
}