#include<stdio.h>
#define max 20
int n;
struct student{
int id;
char name[max];
int score;
int rank;
};
student t1[max];
student t[max];
student stu[max];
void build(student stu[])
{///建立文件
FILE *fp;
int i;
fp=fopen("E:\\123.txt","w");
printf ("输入学生人数:\n"); ///将学生信息写入文件
scanf("%d",&n);
printf("\n输入学生信息(学号,姓名,分数)\n");
for(i=1;i<=n;i++)
{
scanf("%d%s%d",&stu[i].id,stu[i].name,&stu[i].score);
fprintf(fp,"%d %s %d\n",stu[i].id,stu[i].name,stu[i].score);
}
fclose(fp);
}
void show(student stu[])
{
int i;
printf(" 学号 姓名 分数\n");
for(i=1;i<=n;i++)
printf("%2d %4s %2d\n",stu[i].id,stu[i].name,stu[i].score);
}
void show1 (student stu[])
{
int i,j;
j=1;
stu[1].rank=1;
for(i=2;i<=n;i++,j++)
{
if(stu[i].score==stu[i-1].score)
{
stu[i].rank=j;
}
else
stu[i].rank=j+1;
}
printf(" 学号 姓名 分数 名次\n");
for(i=1;i<=n;i++)
printf(" %2d %5s %2d %2d\n",stu[i].id,stu[i].name,stu[i].score,stu[i].rank);
}
void insert_sort(student stu[],int n)
{///直接插入排序
int i,j;
FILE *fp;
fp=fopen("E:\\123.txt","r"); ///打开文件并提取文件内容
for(i=1;i<=n;i++)
{
fscanf (fp,"%d%s%d",&stu[i].id,stu[i].name,&stu[i].score);
}
for(i=2;i<=n;++i)
{
if(stu[i].score>stu[i-1].score)
{
stu[0]=stu[i];
stu[i]=stu[i-1];
for(j=i-2;stu[0].score>stu[j].score;--j)
stu[j+1]=stu[j];
stu[j+1]=stu[0];
}
}
show1(stu);
}
void Bubble_sort(student stu[],int n)
{///冒泡排序
int i,j;
int flag;
student temp;
FILE *fp;
fp=fopen("E:\\123.txt","r");
for(i=1;i<=n;i++)
{
fscanf (fp,"%d%s%d",&stu[i].id,stu[i].name,&stu[i].score);
}
for(i=1;i<=n && flag;i++)
{
flag=0;
for(j=2;j<=n-i;j++)
{
if(stu[j].score>stu[j-1].score)
{
temp=stu[j];
stu[j]=stu[j-1];
stu[j-1]=temp;
flag=1;
}
}
}
show1(stu);
}
int Partition(student stu[],int low,int high)
{
int s;
stu[0]=stu[low];
s=stu[low].score;
while(low<high)
{
while(low<high && stu[high].score<=s)
--high;
stu[low]=stu[high];
while(low<high && stu[low].score>=s)
++low;
stu[high]=stu[low];
}
stu[high]=stu[0];
return low;
}
void Qsort(student stu[],int low,int high)
{
int a;
if(low<high)
{
a=Partition(stu,low,high);
Qsort(stu,low,a-1);
Qsort(stu,a+1,high);
}
}
void Quicksort(student stu[],int n)
{///快速排序
int i;
FILE *fp;
fp=fopen("E:\\123.txt","r");
for(i=1;i<=n;i++)
{
fscanf (fp,"%d%s%d",&stu[i].id,stu[i].name,&stu[i].score);
}
Qsort(stu,1,n);
show1(stu);
}
int Selectmin (student stu[],int m,int n)
{
int k,i,s=-1;
for(i=m;i<=n;i++)
{
if(stu[i].score>s)
{
s=stu[i].score;
k=i;
}
}
return k;
}
void Selectsort(student stu[],int n)
{///简单选择排序
int i,j;
student temp;
FILE *fp;
fp=fopen("E:\\123.txt","r");
for(i=1;i<=n;i++)
{
fscanf (fp,"%d%s%d",&stu[i].id,stu[i].name,&stu[i].score);
}
for(i=1;i<n;i++)
{
j=Selectmin(stu,i,n);
if(i!=j)
{
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
show1(stu);
}
void Heapadjust(student stu[],int s,int m)
{///调整为大顶堆
int i;
student r;
r=stu[s];
for(i=2*s;i<=m;i*=2)
{
if(i<m && stu[i].score>stu[i+1].score)
++i;
if(!(r.score>stu[i].score))
break;
stu[s]=stu[i];
s=i;
}
stu[s]=r;
}
void Heap (student stu[])
{///堆排序
int i;
student temp;
FILE *fp;
fp=fopen("E:\\123.txt","r");
for(i=1;i<=n;i++)
{
fscanf (fp,"%d%s%d",&stu[i].id,stu[i].name,&stu[i].score);
}
for(i=n/2;i>0;--i)
Heapadjust(stu,i,n);
for(i=n;i>1;--i)
{
temp=stu[1];
stu[1]=stu[i];
stu[i]=temp;
Heapadjust(stu,1,i-1);
}
show1(stu);
}
void Merge (student stu[],student t[],int low,int mid,int high)
{///将stu[i...m]和stu[m+1...n]合并到t[i...n]
int i,j,k;
for(i=low,j=mid+1,k=low;i<=mid && j<=high; k++)
{
if(stu[i].score<=stu[j].score)///将stu[].score按从大到小并入t[]中
{
t[k]=stu[i];
i++;
}
else
{
t[k]=stu[j];
j++;
}
}
while(i<=mid)
{
t[k]=stu[i];
k++;
i++;
}
while(j<=high)
{
t[k]=stu[j];
k++;
j++;
}
}
void Msort(student stu[],student t[],int low,int high)
{
int mid;
if(low==high)
t[low]=stu[low];
else
{
mid=(low+high)/2;
Msort (stu,t1,low,mid);
Msort(stu,t1,mid+1,high);
Merge(t1,t,low,mid,high);
}
}
void Mergesort(student stu[])
{
FILE *fp;
int i;
fp=fopen("E:\\123.txt","r");
for(i=1;i<=n;i++)
{
fscanf (fp,"%d%s%d",&stu[i].id,stu[i].name,&stu[i].score);
}
Msort (stu,stu,1,n);
show1(stu);
}
int main()
{
int i,t;
FILE *fp;
fp=fopen("E:\\123.txt","r");
while (1)
{
printf("选择排序方法:\n************\n");
printf("1.录入学生信息\n2.直接插入排序\n3.冒泡排序\n4.快速排序\n5.简单选择排序\n6.堆排序\n7.2-路归并排序\n8.输出学生信息\n0.退出\n");
scanf ("%d",&t);
switch (t)
{
case 1:
{
build(stu);
for(i=1;i<=n;i++)
{
fscanf (fp,"%d%s%d",&stu[i].id,stu[i].name,&stu[i].score);
}
break;
}
case 2: insert_sort(stu,n);break;
case 3: Bubble_sort(stu,n);break;
case 4: Quicksort(stu,n);break;
case 5: Selectsort(stu,n);break;
case 6: Heap(stu);break;
case 7: Mergesort(stu);break;
case 8: show(stu);break;
case 0: return 0;break;
}
}
return 0;
}
排序
最新推荐文章于 2024-04-21 17:41:25 发布