学生成绩排序(直接插入,冒泡,快排,选择,堆排,2路归并)

内容:

给出n个学生的考试成绩表,每条记录由学号、姓名和分数和名次组成,设计算法完成下列操作:

(1)设计一个显示对学生信息操作的菜单函数如下所示:

*************************

       1、录入学生基本信息

       2、直接插入排序

       3、冒泡排序

       4、快速排序

       5、简单选择排序

       6、堆排序

       7、2-路归并排序

       8、输出学生信息

       0、退出

*************************

请选择:

算法设计要求:按分数从高到低的顺序进行排序,分数相同的为同一名次。输入的学生信息存入文件中,每选择一种排序方法,必须从文件中取出数据。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n;
typedef struct student{
	char num[20];
	char name[25];
	int score;	
}*Stu,StuNode;

void Build(Stu stu)
{
	FILE *fp;
	char name[20];
	int score,i;
	char num[20];
	fp=fopen("E:\\Student.txt","w");
	printf("请输入学生人数:");
	scanf("%d",&n);

	printf("请输入学生信息(学号,姓名,成绩):\n");
	for(i=1;i<=n;i++)
	{
		scanf("%s %s %d",num,name,&score);
		fprintf(fp,"%s %s %d\n",num,name,score);
	}
	printf("登录成功.\n");
	fclose(fp);
}
void ReadFile(Stu &stu)
{//读文件,存到Stu结构体中 
	FILE *fp;
	int i;
	stu=(Stu)malloc(sizeof(StuNode)*(n+1));
	fp=fopen("E:\\Student.txt","r");
	for(i=1;i<=n;i++)
		fscanf(fp,"%s %s %d",stu[i].num,stu[i].name,&stu[i].score);

	fclose(fp);
}

void Display(Stu stu)
{//显示数据 
	int i;
	int temp=1,index=0;
	for(i=1;i<=n;i++)
	{
		printf("%2d %15s %15s %5d\n",temp,stu[i].num,stu[i].name,stu[i].score);
		if(i<n && stu[i].score==stu[i+1].score)
			index++;		
		else
		{
			temp+=index+1;
			index=0;
		}
	}
	
	free(stu);
}

void Swap(StuNode &a,StuNode &b)
{
	StuNode temp;
	temp=a;
	a=b;
	b=temp;
}

void InsertSort(Stu stu)
{//直接插入排序
	int i,j;
	int key;
	char name[20];
	char num[20];
	StuNode temp;
	stu=(Stu)malloc(sizeof(StuNode)*(n+1));
	ReadFile(stu);
	
	for(i=n;i>=1;i--)
	{
		temp=stu[i];
		j=i-1;
		while(j>=1 && temp.score> stu[j].score)
		{
			stu[j+1]=stu[j];
			j--;
		}
		j+=1;
		stu[j]=temp;
	}
	Display(stu);
}

void BubbleSort(Stu stu)
{//冒泡排序 
	int i,j,peace;
	stu=(Stu)malloc(sizeof(StuNode)*(n+1));
	ReadFile(stu);
	peace=1;
	for(i=1;i<=n-1 && peace;i++)
	{
		peace=0;
		for(j=1;j<=n-j;j++)
		{
			if(stu[j].score < stu[j+1].score)
			{
				Swap(stu[j],stu[j+1]);
				peace=1;
			}
		}
	}
	Display(stu);
}
int Partition(Stu stu,int low,int high)
{//快速排序--排序 
	int Pi;
	stu[0]=stu[low];
	
	Pi=stu[0].score;
	while(low<high)
	{
		while(low<high && stu[high].score<=Pi)
			high--;
		stu[low]=stu[high];
		while(low<high && stu[low].score>=Pi)
			low++;
		stu[high]=stu[low];
	}
	stu[low]=stu[0];
	return low;
}
void QuickSort(Stu stu,int low,int high)
{//快速排序--二分 
	int Pi;
	if(low<high)
	{
		Pi=Partition(stu,low,high);
		QuickSort(stu,low,Pi-1);
		QuickSort(stu,Pi+1,high);
	}
}
void SeletionSort(Stu stu)
{//选择排序 
	int i,j,temp;
	stu=(Stu)malloc(sizeof(StuNode)*(n+1));
	ReadFile(stu);
	for(i=1;i<=n-1;i++)
	{
		int key=stu[i].score;
		for(j=i+1;j<=n;j++)
		{
			if(key<stu[j].score)
			{
				temp=j;
				key=stu[j].score;
			}
		}
		if(i!=j)
			Swap(stu[i],stu[temp]);
	}
	Display(stu);
}
void HeapAdjust(Stu stu,int s,int m)
{//调整堆 
	int j;
	StuNode rc;
	rc=stu[s];
	
	for(j=2*s;j<=m;j*=2)
	{
		if(j<m && stu[j].score>stu[j+1].score)
			j++;
		if(!(rc.score>stu[j].score))
			break;
		stu[s]=stu[j];
			s=j;
	}
	stu[s]=rc;
}
void HeapSort(Stu stu)
{//堆排序 
	int i;
	ReadFile(stu);
	for(i=n/2;i>0;i--)
		HeapAdjust(stu,i,n);
	for(i=n;i>1;i--)
	{
		Swap(stu[1],stu[i]);
		HeapAdjust(stu,1,i-1);
	}
	Display(stu);
}

void Merge(Stu stu,int low,int mid,int high)
{//归并排序--归并 
	if(low>=high)
		return ;
	int i,j,k;
	StuNode temp[1005];
	i=low;j=mid+1;k=0;
	while(i<=mid && j<=high)
	{
		if(stu[i].score>stu[j].score)
			temp[k++]=stu[i++];
		else
			temp[k++]=stu[j++];
	}
	while(i<=mid)
		temp[k++]=stu[i++];
	while(j<=high)
		temp[k++]=stu[j++];
	for(i=low;i<=high;i++)
	{
		stu[i]=temp[i-low];
		printf("%d ",stu[i].score);
	}
	putchar('\n');
	
}

void MergeSort(Stu stu,int low,int high)
{//归并排序--二分 
	if(low>=high)
		return ;
	int mid=(low+high)/2;
	MergeSort(stu,low,mid);
	MergeSort(stu,mid+1,high);
	Merge(stu,low,mid,high);
}

int main()
{
	Stu stu;
	int key;
	while(1)
	{
		printf("	----------------------\n");
		printf("	1.录入学生基本信息。\n");
		printf("	2.直接插入排序。\n");
		printf("	3.冒泡排序。\n");
		printf("	4.快速排序。\n"); 
		printf("	5.简单选择排序。\n");
		printf("	6.堆排序。\n");
		printf("	7.2-路归并排序。\n");
		printf("	8.输出学生信息。\n");
		printf("	9.退出。\n");
		printf("	----------------------\n");
		scanf("%d",&key);
				
		switch(key)
		{
			case 1:
				Build(stu);
				break;
			case 2:
				InsertSort(stu);
				break;
			case 3:
				BubbleSort(stu);
				break;
			case 4:
				ReadFile(stu);
				QuickSort(stu,1,n);
				Display(stu);
				break;
			case 5:
				SeletionSort(stu);
				break;
			case 6:
				HeapSort(stu);
				break;
			case 7:
				ReadFile(stu);
				MergeSort(stu,1,n);
				Display(stu);
				break;
			case 8:
				ReadFile(stu);
				Display(stu);
				break;
			default:
				return 0;
		}
	}
}


  • 12
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
1. 直接插入排序:将待排序的数列分为有序区和无序区,从无序区中取出一个元素,插入到有序区中的正确位置,直到无序区为空。时间复杂度为O(n^2)。 2. 希尔排序:将待排序的数列分成若干个子序列,对每个子序列进行插入排序,缩小增量,直到增量为1,最后对整个数列进行一次插入排序。时间复杂度为O(nlogn)。 3. 冒泡排序:比较相邻的两个元素,如果前者大于后者,则交换它们的位置,一次冒泡可以把一个元素排到正确的位置,重复进行n-1次,直到排序完成。时间复杂度为O(n^2)。 4. 快速排序:选定一个基准值,将数列分为左右两个部分,左边的元素都小于基准值,右边的元素都大于基准值,递归地对左右两个部分进行快速排序。时间复杂度为O(nlogn)。 5. 简单选择排序:从待排序的数列中选择最小的元素,放置到已排序数列的末尾,重复该过程,直到待排序数列为空。时间复杂度为O(n^2)。 6. 堆排序:先将待排序的数列构建成一个最大堆,然后将堆顶的元素与末尾元素交换位置,并重新构建最大堆,重复该过程,直到排序完成。时间复杂度为O(nlogn)。 7. 归并排序:将待排序的数列分成两个部分,对每个部分进行归并排序,然后将两个有序的部分合并成一个有序的数列,递归地进行该过程,直到排序完成。时间复杂度为O(nlogn)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值