1. 程式人生 > >實驗六 排序的有關操作(資料結構)

實驗六 排序的有關操作(資料結構)

實驗內容:

給出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;

}

測試截圖: