第十六週上機實踐—項目1(3)—驗證算法 堆排序 歸併排序 基數排序

/*      
 *Copyright(c) 2015,煙臺大學計算機學院      
 *All rights reserved.      
 *文件名稱:test.cpp      
 *作者:林莉      
 *完成日期:2015年12月14日      
 *版本:v1.0      
 *      
 *問題描述:用序列{57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7}作爲測試數據,運行並本週視頻中所講過的算法對應 程
 序,觀察運行結果並深刻領會算法的思路和實現方法:堆排序 歸併排序 基數排序
  
 *輸入描述:無      
 *輸出描述:所得結果。   
 */ 

1.堆排序

#include <stdio.h>
#define MaxSize 20
typedef int KeyType;    //定義關鍵字類型
typedef char InfoType[10];
typedef struct          //記錄類型
{
    KeyType key;        //關鍵字項
    InfoType data;      //其他數據項,類型爲InfoType
} RecType;              //排序的記錄類型定義

//調整堆
void sift(RecType R[],int low,int high)
{
    int i=low,j=2*i;                        //R[j]是R[i]的左孩子
    RecType temp=R[i];
    while (j<=high)
    {
        if (j<high && R[j].key<R[j+1].key)  //若右孩子較大,把j指向右孩子
            j++;                                //變爲2i+1
        if (temp.key<R[j].key)
        {
            R[i]=R[j];                          //將R[j]調整到雙親結點位置上
            i=j;                                //修改i和j值,以便繼續向下篩選
            j=2*i;
        }
        else break;                             //篩選結束
    }
    R[i]=temp;                                  //被篩選結點的值放入最終位置
}

//堆排序
void HeapSort(RecType R[],int n)
{
    int i;
    RecType temp;
    for (i=n/2; i>=1; i--) //循環建立初始堆
        sift(R,i,n);
    for (i=n; i>=2; i--) //進行n-1次循環,完成推排序
    {
        temp=R[1];       //將第一個元素同當前區間內R[1]對換
        R[1]=R[i];
        R[i]=temp;
        sift(R,1,i-1);   //篩選R[1]結點,得到i-1個結點的堆
    }
}

int main()
{
    int i,n=12;
    RecType R[MaxSize];
    KeyType a[]= {57,40,38,11,13,34,48,75,6,19,9,7};//a[0]空閒,不作爲關鍵字
    for (i=1; i<=n; i++)
        R[i].key=a[i];
    printf("排序前:");
    for (i=1; i<=n; i++)
        printf("%d ",R[i].key);
    printf("\n");
    HeapSort(R,n);
    printf("排序後:");
    for (i=1; i<=n; i++)
        printf("%d ",R[i].key);
    printf("\n");
    return 0;
}


 

2.歸併排序

#include <stdio.h>
#include <malloc.h>
#define MaxSize 20
typedef int KeyType;    //定義關鍵字類型
typedef char InfoType[10];
typedef struct          //記錄類型
{
    KeyType key;        //關鍵字項
    InfoType data;      //其他數據項,類型爲InfoType
} RecType;              //排序的記錄類型定義

void Merge(RecType R[],int low,int mid,int high)
{
    RecType *R1;
    int i=low,j=mid+1,k=0; //k是R1的下標,i、j分別爲第1、2段的下標
    R1=(RecType *)malloc((high-low+1)*sizeof(RecType));  //動態分配空間
    while (i<=mid && j<=high)       //在第1段和第2段均未掃描完時循環
        if (R[i].key<=R[j].key)     //將第1段中的記錄放入R1中
        {
            R1[k]=R[i];
            i++;
            k++;
        }
        else                            //將第2段中的記錄放入R1中
        {
            R1[k]=R[j];
            j++;
            k++;
        }
    while (i<=mid)                      //將第1段餘下部分複製到R1
    {
        R1[k]=R[i];
        i++;
        k++;
    }
    while (j<=high)                 //將第2段餘下部分複製到R1
    {
        R1[k]=R[j];
        j++;
        k++;
    }
    for (k=0,i=low; i<=high; k++,i++) //將R1複製回R中
        R[i]=R1[k];
}

void MergePass(RecType R[],int length,int n)    //對整個數序進行一趟歸併
{
    int i;
    for (i=0; i+2*length-1<n; i=i+2*length)     //歸併length長的兩相鄰子表
        Merge(R,i,i+length-1,i+2*length-1);
    if (i+length-1<n)                       //餘下兩個子表,後者長度小於length
        Merge(R,i,i+length-1,n-1);          //歸併這兩個子表
}
void MergeSort(RecType R[],int n)           //自底向上的二路歸併算法
{
    int length;
    for (length=1; length<n; length=2*length) //進行log2n趟歸併
        MergePass(R,length,n);
}
int main()
{
    int i,n=12;
    RecType R[MaxSize];
    KeyType a[]= {57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7};
    for (i=0; i<n; i++)
        R[i].key=a[i];
    printf("排序前:");
    for (i=0; i<n; i++)
        printf("%d ",R[i].key);
    printf("\n");
    MergeSort(R,n);
    printf("排序後:");
    for (i=0; i<n; i++)
        printf("%d ",R[i].key);
    printf("\n");
    return 0;
}

運行結果:

3.基數排序

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAXE 20         //線性表中最多元素個數
#define MAXR 10         //基數的最大取值
#define MAXD 8          //關鍵字位數的最大取值
typedef struct node
{
    char data[MAXD];    //記錄的關鍵字定義的字符串
    struct node *next;
} RecType;
void CreaLink(RecType *&p,char *a[],int n);
void DispLink(RecType *p);
void RadixSort(RecType *&p,int r,int d) //實現基數排序:*p爲待排序序列鏈表指針,r爲基數,d爲關鍵字位數
{
    RecType *head[MAXR],*tail[MAXR],*t; //定義各鏈隊的首尾指針
    int i,j,k;
    for (i=0; i<=d-1; i++)                  //從低位到高位循環
    {
        for (j=0; j<r; j++)                 //初始化各鏈隊首、尾指針
            head[j]=tail[j]=NULL;
        while (p!=NULL)                 //對於原鏈表中每個結點循環
        {
            k=p->data[i]-'0';           //找第k個鏈隊
            if (head[k]==NULL)          //進行分配
            {
                head[k]=p;
                tail[k]=p;
            }
            else
            {
                tail[k]->next=p;
                tail[k]=p;
            }
            p=p->next;                  //取下一個待排序的元素
        }
        p=NULL;                         //重新用p來收集所有結點
        for (j=0; j<r; j++)             //對於每一個鏈隊循環
            if (head[j]!=NULL)          //進行收集
            {
                if (p==NULL)
                {
                    p=head[j];
                    t=tail[j];
                }
                else
                {
                    t->next=head[j];
                    t=tail[j];
                }
            }
        t->next=NULL;                   //最後一個結點的next域置NULL
        //以下的顯示並非必要
        printf("  按%d位排序\t",i);
        DispLink(p);
    }
}
void CreateLink(RecType *&p,char a[MAXE][MAXD],int n)   //採用後插法產生鏈表
{
    int i;
    RecType *s,*t;
    for (i=0; i<n; i++)
    {
        s=(RecType *)malloc(sizeof(RecType));
        strcpy(s->data,a[i]);
        if (i==0)
        {
            p=s;
            t=s;
        }
        else
        {
            t->next=s;
            t=s;
        }
    }
    t->next=NULL;
}
void DispLink(RecType *p)   //輸出鏈表
{
    while (p!=NULL)
    {
        printf("%c%c ",p->data[1],p->data[0]);
        p=p->next;
    }
    printf("\n");
}
int main()
{
    int n=12,r=10,d=2;
    int i,j,k;
    RecType *p;
    char a[MAXE][MAXD];
    int b[]= {57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7};
    for (i=0; i<n; i++)     //將b[i]轉換成字符串
    {
        k=b[i];
        for (j=0; j<d; j++) //例如b[0]=75,轉換後a[0][0]='7',a[0][1]='5'
        {
            a[i][j]=k%10+'0';
            k=k/10;
        }
        a[i][j]='\0';
    }
    CreateLink(p,a,n);
    printf("\n");
    printf("  初始關鍵字\t");        //輸出初始關鍵字序列
    DispLink(p);
    RadixSort(p,10,2);
    printf("  最終結果\t");         //輸出最終結果
    DispLink(p);
    printf("\n");
    return 0;
}


運行結果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章