pat 1028

本文详细介绍了如何使用C语言和结构体实现学生信息的排序,并通过自定义比较函数实现了多种排序方式。代码示例涵盖了基本的数据结构应用、排序算法原理以及在实际场景中的应用。

看了网上的答案,用了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;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值