看了网上的答案,用了qsort过了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct list{
char id[10];
int grade;
char name[15];
} stu[1000005];
int c,n;
int compare(const void *atmp,const void *btmp){
struct list *a=(struct list *)atmp;
struct list *b=(struct list *)btmp;
if(c == 1)
return strcmp(a->id,b->id);
else if(c == 2){
if(strcmp(a->name,b->name))
return strcmp(a->name,b->name);
else return strcmp(a->id,b->id);
}
else if(c == 3){
if(a->grade == b->grade)
return strcmp(a->id,b->id);
else return a->grade - b->grade;
}
}
/*
void swamp(struct list *a,struct list *b){
struct list tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
struct list median3(struct list a[],int begin,int end,int flag){
int mid;
mid = (begin + end)/2;
if(compare(a[begin],a[mid],flag)>0)
swamp(&a[begin],&a[mid]);
if(compare(a[begin],a[end],flag)>0)
swamp(&a[begin],&a[end]);
if(compare(a[mid],a[end],flag)>0)
swamp(&a[mid],&a[end]);
swamp(&a[mid],&a[end-1]);
return a[end-1];
}
void Insertsort(struct list a[],int n,int flag){
int i,j;
struct list tmp;
for(i = 1;i<n;i++){
tmp = a[i];
for(j = i;compare(tmp,a[j-1],flag)<0&&j>0;j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
void partition(struct list a[],int begin,int end,int flag){
int i,j;
struct list pivot;
if(end - begin>1){
pivot = median3(a,begin,end,flag);
i = 0;
j = end-1;
while(1){
while(compare(a[++i],pivot,flag)<0);
while(compare(a[--j],pivot,flag)>0);
if(i<j)
swamp(&a[i],&a[j]);
else break;
}
swamp(&a[i],&a[end-1]);
partition(a,begin,i-1,flag);
partition(a,i+1,end,flag);
}
else if(end>begin&&compare(a[begin],a[end],flag)>0) swamp(&a[begin],&a[end]); //the array start from a + begin
}
void quicksort(struct list a[],int n,int flag){
partition(a,0,n-1,flag);
}
*/
int main(){
int i;
scanf("%d%d",&n,&c);
//stu = (struct list*)malloc(sizeof(struct list)*(n+5));
for(i = 0;i<n;i++)
scanf("%s %s %d",stu[i].id,stu[i].name,&stu[i].grade);
qsort(stu,n,sizeof(struct list),compare);
//quicksort(stu,n,c);
for(i = 0;i<n;i++)
printf("%s %s %d\n",stu[i].id,stu[i].name,stu[i].grade);
//free(stu);
return 0;
}原代码没用qsort
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct list{
char id[10];
int grade;
char name[15];
} stu[1000005];
int compare(struct list a,struct list b,int flag){
if(flag == 1)
return strcmp(a.id,b.id);
else if(flag == 2){
if(strcmp(a.name,b.name))
return strcmp(a.name,b.name);
else return strcmp(a.id,b.id);
}
else if(flag == 3){
if(a.grade == b.grade)
return strcmp(a.id,b.id);
else return a.grade - b.grade;
}
}
void swamp(struct list *a,struct list *b){
struct list tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
struct list median3(struct list a[],int begin,int end,int flag){
int mid;
mid = (begin + end)/2;
if(compare(a[begin],a[mid],flag)>0)
swamp(&a[begin],&a[mid]);
if(compare(a[begin],a[end],flag)>0)
swamp(&a[begin],&a[end]);
if(compare(a[mid],a[end],flag)>0)
swamp(&a[mid],&a[end]);
swamp(&a[mid],&a[end-1]);
return a[end-1];
}
void Insertsort(struct list a[],int n,int flag){
int i,j;
struct list tmp;
for(i = 1;i<n;i++){
tmp = a[i];
for(j = i;compare(tmp,a[j-1],flag)<0&&j>0;j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
void partition(struct list a[],int begin,int end,int flag){
int i,j;
struct list pivot;
if(end - begin>1){
pivot = median3(a,begin,end,flag);
i = 0;
j = end-1;
while(1){
while(compare(a[++i],pivot,flag)<0);
while(compare(a[--j],pivot,flag)>0);
if(i<j)
swamp(&a[i],&a[j]);
else break;
}
swamp(&a[i],&a[end-1]);
partition(a,begin,i-1,flag);
partition(a,i+1,end,flag);
}
else if(end>begin&&compare(a[begin],a[end],flag)>0) swamp(&a[begin],&a[end]); //the array start from a + begin
}
void quicksort(struct list a[],int n,int flag){
partition(a,0,n-1,flag);
}
int main(){
int n,c,i;
scanf("%d%d",&n,&c);
//stu = (struct list*)malloc(sizeof(struct list)*(n+5));
for(i = 0;i<n;i++)
scanf("%s %s %d",stu[i].id,stu[i].name,&stu[i].grade);
quicksort(stu,n,c);
for(i = 0;i<n;i++)
printf("%s %s %d\n",stu[i].id,stu[i].name,stu[i].grade);
//free(stu);
return 0;
}
本文详细介绍了如何使用C语言和结构体实现学生信息的排序,并通过自定义比较函数实现了多种排序方式。代码示例涵盖了基本的数据结构应用、排序算法原理以及在实际场景中的应用。
454

被折叠的 条评论
为什么被折叠?



