第十六周 项目4-英文单词的基数排序

来源:互联网 发布:酷狗音乐显示网络异常 编辑:程序博客网 时间:2024/04/30 04:16

问题及代码:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. /*           
  2. *烟台大学计算机与控制工程学院            
  3. *作    者:  朱建豪  
  4. *完成日期:2016年12月30日        
  5. *问题描述:设计一个基数排序的算法,将一组英文单词,按字典顺序排列。假设单词均由小写字母或空格构成,最长的单词有MaxLen个字母。   
  6. */     

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>    
  2. #include <malloc.h>    
  3. #include <string.h>    
  4. #define MaxLen 9                //单词的最大长度    
  5. #define Radix  27               //基数rd为27,分别对应' ','a',…'z'    
  6. typedef char String[MaxLen+1];  //定义String为字符数组类型    
  7. typedef struct node    
  8. {    
  9.     String word;    
  10.     struct node *next;    
  11. } LinkNode;    
  12. void DispWord(String R[],int n) //输出单词    
  13. {    
  14.     int i;    
  15.     printf("  ");    
  16.     for (i=0; i<n; i++)    
  17.         printf("[%s] ",R[i]);    
  18.     printf("\n");    
  19. }    
  20. void PreProcess(String R[],int n)    
  21. //对单词进行预处理,用空格填充尾部至MaxLen长    
  22. {    
  23.     int i,j;    
  24.     for (i=0; i<n; i++)    
  25.     {    
  26.         if (strlen(R[i])<MaxLen)    
  27.         {    
  28.             for (j=strlen(R[i]); j<MaxLen; j++)    
  29.                 R[i][j]=' ';    
  30.             R[i][j]='\0';    
  31.         }    
  32.     }    
  33. }    
  34. void EndProcess(String R[],int n)    
  35. //恢复处理,删除预处理时填充的尾部空格    
  36. {    
  37.     int i,j;    
  38.     for (i=0; i<n; i++)    
  39.     {    
  40.         for (j=MaxLen-1; R[i][j]==' '; j--);    
  41.         R[i][j+1]='\0';    
  42.     }    
  43. }    
  44. void Distribute(String R[],LinkNode *head[],LinkNode *tail[],int j,int n)    
  45. //按关键字的第j个分量进行分配,进入此过程时各队列一定为空    
  46. {    
  47.     int i,k;    
  48.     LinkNode *p;    
  49.     for (i=0; i<n; i++)         //依次扫描R[i],将其入队    
  50.     {    
  51.         if (R[i][j]==' ')       //空格时放入0号队列中,'a'时放入1号队列中,…    
  52.             k=0;    
  53.         else    
  54.             k=R[i][j]-'a'+1;    
  55.         p=(LinkNode *)malloc(sizeof(LinkNode)); //创建新结点    
  56.         strcpy(p->word,R[i]);    
  57.         p->next=NULL;    
  58.         if (head[k]==NULL)    
  59.         {    
  60.             head[k]=p;    
  61.             tail[k]=p;    
  62.         }    
  63.         else    
  64.         {    
  65.             tail[k]->next=p;    
  66.             tail[k]=p;    
  67.         }    
  68.     }    
  69. }    
  70. void Collect(String R[],LinkNode *head[])    
  71. //依次将各非空队列中的记录收集起来    
  72. {    
  73.     int k=0,i;    
  74.     LinkNode *p;    
  75.     for (i=0; i<Radix; i++)    
  76.         for (p=head[i]; p!=NULL; p=p->next)    
  77.             strcpy(R[k++],p->word);    
  78. }    
  79. void RadixSort(String R[],int n)    //对R[0..n-1]进行基数排序    
  80. {    
  81.     LinkNode *head[Radix],*tail[Radix]; //定义Radix个队列    
  82.     int i,j;    
  83.     for (i=MaxLen-1; i>=0; i--)             //从低位到高位做d趟箱排序    
  84.     {    
  85.         for (j=0; j<Radix; j++)    
  86.             head[j]=tail[j]=NULL;           //队列置空    
  87.         Distribute(R,head,tail,i,n);        //第i趟分配    
  88.         Collect(R,head);                    //第i趟收集    
  89.     }    
  90. }    
  91. int main()    
  92. {    
  93.     int n=6;    
  94.     String R[]= {"while","if","if else","do while","for","case"};    
  95.     printf("排序前:\n");    
  96.     DispWord(R,n);    
  97.     PreProcess(R,n);    
  98.     printf("预处理后:\n");    
  99.     DispWord(R,n);    
  100.     RadixSort(R,n);    
  101.     printf("排序结果:\n");    
  102.     DispWord(R,n);    
  103.     EndProcess(R,n);    
  104.     printf("最终结果:\n");    
  105.     DispWord(R,n);    
  106.     printf("\n");    
  107.     return 0;    
  108. }    

运行结果:

0 0
原创粉丝点击