排序的有关操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CJL2313/article/details/79043965

实验六

实验名称:排序的有关操作

实验室名称:丹青909

实验台号:14

学生姓名: 陈佳龙

专业班级: 2015级1班

指导教师:于慧伶

实验日期:2017-6-12

一、实验目的

1、掌握常见的排序算法的思想及其适用条件。

2、掌握常见的排序算法的程序实现。

二、实验仪器及环境:

    PC计算机;windows XP操作系统Visual C++6.0、CodeBlocks

三、实验内容及结果(按照具体实验题目,按照如下格式书写)

 1、

#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;

}

1、

四、实验心得体会:(包括遇到的问题及解决办法)

掌握各种排序方法的时间和空间复杂度分析,分析出排序算法最好情况,最坏情况,和平均情况的时间性能。

五、指导教师意见及成绩

                                  签名:

                                                 

猜你喜欢

转载自blog.csdn.net/CJL2313/article/details/79043965
今日推荐