实验六 排序的有关操作(数据结构)
实验内容:
给出n个学生的考试成绩表,每条信息由姓名和分数组成,试设计一个算法:
按分数高低次序,打印出每个学生在考试中获得的名次,分数相同的为同一名次;按名次列出每个学生的姓名与分数。
要求至少使用3种以上的排序方法(必须包含快速排序算法)。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXSIZE 200
using namespace std;
typedef struct student{
char name[25];
double score;
}*Stu,StuNode;
StuNode r[MAXSIZE+1],dp[MAXSIZE+1];
int n=0;
void Cread(){
for(int i=1;i<=n;i++)
{
cout<<"请输入第"<<i<<"个学生的姓名和分数:";
cin>>r[i].name>>r[i].score;
}
}
void Show(Stu stu){
int i;
int temp=1,index=0;
for(i=1;i<=n;i++)
{
printf("%2d %15s %5.2f\n",temp,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 InsertSort(Stu stu){//直接插入排序
int j;
StuNode temp;
stu=(Stu)malloc(sizeof(StuNode)*(n+1));
for(int i=1;i<=n;i++){
strcpy(stu[i].name,r[i].name);
stu[i].score=r[i].score;
}
for(int i=2;i<=n;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;
}
Show(stu);
}
void Swap(StuNode &a,StuNode &b)
{
StuNode temp;
strcpy(temp.name,a.name);
strcpy(a.name,b.name);
strcpy(b.name,temp.name);
temp.score=a.score;
a.score=b.score;
b.score=temp.score;
}
void BubbleSort(Stu stu){//冒泡排序
int i,j,peace;
peace=1;
stu=(Stu)malloc(sizeof(StuNode)*(n+1));
for(i=1;i<=n;i++){
strcpy(stu[i].name,r[i].name);
stu[i].score=r[i].score;
}
peace=1;
for(i=1;i<=n-1 && peace;i++)
{
peace=0;
for(j=1;j<=n-i;j++)
{
if(stu[j].score < stu[j+1].score)
{
Swap(stu[j],stu[j+1]);
peace=1;
}
}
}
Show(stu);
}
void SeletionSort(Stu stu){//选择排序
int i,j,temp;
stu=(Stu)malloc(sizeof(StuNode)*(n+1));
for(i=1;i<=n;i++){
strcpy(stu[i].name,r[i].name);
stu[i].score=r[i].score;
}
for(i=1;i<=n-1;i++)
{
int key=i;
for(j=i+1;j<=n;j++)
{
if(stu[key].score<stu[j].score)
{
key=j;
}
}
if(i!=key)
Swap(stu[i],stu[key]);
}
Show(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 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;
cout<<"请输入学生总人数n:";
cin>>n;
Cread();
cout<<" 冒泡法排序 :"<<endl;
BubbleSort(stu);
cout<<" 直接插入法排序 :"<<endl;
InsertSort(stu);
cout<<" 选择法排序 :"<<endl;
SeletionSort(stu);
cout<<" 快速法排序 :"<<endl;
stu=(Stu)malloc(sizeof(StuNode)*(n+1));
for(int i=1;i<=n;i++){
strcpy(stu[i].name,r[i].name);
stu[i].score=r[i].score;
}
QuickSort(stu,1,n);
Show(stu);
cout<<" 归并法排序 :"<<endl;
stu=(Stu)malloc(sizeof(StuNode)*(n+1));
for(int i=1;i<=n;i++){
strcpy(stu[i].name,r[i].name);
stu[i].score=r[i].score;
}
MergeSort(stu,1,n);
Show(stu);
return 0;
}
测试截图: