2021-06-15

排序的基本操作与应用

代码源如下,可以运行

算法要求:

  1. 掌握常用的排序方法,并掌握用高级语言实现排序算法的方法;
  2. 深刻理解排序的定义和各种排序方法的特点,并能加以灵活应用;
  3. 了解各种方法的排序过程及其时间复杂度的分析方法。
#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;  
        }  
    }  
}
 显示效果图如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CJ_小前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值