第十六周项目1-(8)验证算法基数排序

来源:互联网 发布:淘宝卖家如何备注订单 编辑:程序博客网 时间:2024/06/07 22:26

问题及代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*      
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院      
  3. * All rights reserved.      
  4. * 文件名称:项目1.cpp      
  5. * 作    者:陈晓琳 
  6. * 完成日期:2016年12月16日      
  7. * 版 本 号:v1.0      
  8.       
  9. * 问题描述:验证基数排序      
  10.       
  11. * 输入描述:无      
  12. * 程序输出:测试数据      
  13. */          

基数排序代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>      
  2. #include <malloc.h>      
  3. #include <string.h>      
  4. #define MAXE 20         //线性表中最多元素个数      
  5. #define MAXR 10         //基数的最大取值      
  6. #define MAXD 8          //关键字位数的最大取值      
  7. typedef struct node      
  8. {      
  9.     char data[MAXD];    //记录的关键字定义的字符串      
  10.     struct node *next;      
  11. } RecType;      
  12. void CreaLink(RecType *&p,char *a[],int n);      
  13. void DispLink(RecType *p);      
  14. void RadixSort(RecType *&p,int r,int d) //实现基数排序:*p为待排序序列链表指针,r为基数,d为关键字位数      
  15. {      
  16.     RecType *head[MAXR],*tail[MAXR],*t; //定义各链队的首尾指针      
  17.     int i,j,k;      
  18.     for (i=0; i<=d-1; i++)                  //从低位到高位循环      
  19.     {      
  20.         for (j=0; j<r; j++)                 //初始化各链队首、尾指针      
  21.             head[j]=tail[j]=NULL;      
  22.         while (p!=NULL)                 //对于原链表中每个结点循环      
  23.         {      
  24.             k=p->data[i]-'0';           //找第k个链队      
  25.             if (head[k]==NULL)          //进行分配      
  26.             {      
  27.                 head[k]=p;      
  28.                 tail[k]=p;      
  29.             }      
  30.             else      
  31.             {      
  32.                 tail[k]->next=p;      
  33.                 tail[k]=p;      
  34.             }      
  35.             p=p->next;                  //取下一个待排序的元素      
  36.         }      
  37.         p=NULL;                         //重新用p来收集所有结点      
  38.         for (j=0; j<r; j++)             //对于每一个链队循环      
  39.             if (head[j]!=NULL)          //进行收集      
  40.             {      
  41.                 if (p==NULL)      
  42.                 {      
  43.                     p=head[j];      
  44.                     t=tail[j];      
  45.                 }      
  46.                 else      
  47.                 {      
  48.                     t->next=head[j];      
  49.                     t=tail[j];      
  50.                 }      
  51.             }      
  52.         t->next=NULL;                   //最后一个结点的next域置NULL      
  53.         //以下的显示并非必要      
  54.         printf("  按%d位排序\t",i);      
  55.         DispLink(p);      
  56.     }      
  57. }      
  58. void CreateLink(RecType *&p,char a[MAXE][MAXD],int n)   //采用后插法产生链表      
  59. {      
  60.     int i;      
  61.     RecType *s,*t;      
  62.     for (i=0; i<n; i++)      
  63.     {      
  64.         s=(RecType *)malloc(sizeof(RecType));      
  65.         strcpy(s->data,a[i]);      
  66.         if (i==0)      
  67.         {      
  68.             p=s;      
  69.             t=s;      
  70.         }      
  71.         else      
  72.         {      
  73.             t->next=s;      
  74.             t=s;      
  75.         }      
  76.     }      
  77.     t->next=NULL;      
  78. }      
  79. void DispLink(RecType *p)   //输出链表      
  80. {      
  81.     while (p!=NULL)      
  82.     {      
  83.         printf("%c%c ",p->data[1],p->data[0]);      
  84.         p=p->next;      
  85.     }      
  86.     printf("\n");      
  87. }      
  88. int main()      
  89. {      
  90.     int n=10,r=10,d=2;      
  91.     int i,j,k;      
  92.     RecType *p;      
  93.     char a[MAXE][MAXD];      
  94.     int b[]= {75,23,98,44,57,12,29,64,38,82};      
  95.     for (i=0; i<n; i++)     //将b[i]转换成字符串      
  96.     {      
  97.         k=b[i];      
  98.         for (j=0; j<d; j++) //例如b[0]=75,转换后a[0][0]='7',a[0][1]='5'      
  99.         {      
  100.             a[i][j]=k%10+'0';      
  101.             k=k/10;      
  102.         }      
  103.         a[i][j]='\0';      
  104.     }      
  105.     CreateLink(p,a,n);      
  106.     printf("\n");      
  107.     printf("  初始关键字\t");        //输出初始关键字序列      
  108.     DispLink(p);      
  109.     RadixSort(p,10,2);      
  110.     printf("  最终结果\t");         //输出最终结果      
  111.     DispLink(p);      
  112.     printf("\n");      
  113.     return 0;      
  114. }      
运行结果:


0 0