将10个成绩排序java程序_快速排序——成绩排序
发布日期:2021-06-24 17:18:45 浏览次数:2 分类:技术文章

本文共 1523 字,大约阅读时间需要 5 分钟。

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;

}

转载地址:https://blog.csdn.net/weixin_34061793/article/details/114720926 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java+map申明_Java中两种HashMap申明方式区别?
下一篇:rust蓝卡怎么开_Rust娘个人资料简介,角色作品介绍

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月13日 01时23分39秒