首页>> 正文

数据结构实践——大数据集上排序算法性能的体验

来源:商群邮件营销时间:2015-09-01 10:36:42点击:2520

本文是针对[ 数据结构基础系列(9):排序 ]的实践项目。

【项目 - 大数据集上排序算法性能的体验】设计一个函数,产生一个至少5万条记录的数据集合。在同一数据集上,用直接插入排序、冒泡排序、快速排序、直接选择排序、堆排序、归并排序、基数排序等算法进行排序,记录所需要的时间,经过对比,得到对复杂度不同的各种算法在运行时间方面的感性认识。

提示1:这一项目需要整合多种排序算法,可以考虑先建设排序算法库,作为我们这门课算法库的收官之作;

提示2:本项目旨在获得对于复杂度不同算法的感性认识,由于数据分布特点、计算机运行状态等不同,其结果并不能完全代替对算法复杂度的理论分析;

提示3:由于C语言标准提供的时间函数只精确到秒,几种O(nlog2n)级别的算法,在5万条记录的压力下,并不能明显地看出优劣,可以忽略直接插入排序、冒泡排序、直接选择排序这三种相对低效率的算法(以节约时间。若能够忍受他们长时间地运行,请自便。),成10倍地加大数据量,然后进行观察。

[参考解答]1.测试用的主控程序——main.cpp

<span class="hljs-comment">#include <stdio.h></span> <span class="hljs-comment">#include <malloc.h></span> <span class="hljs-comment">#include <stdlib.h></span> <span class="hljs-comment">#include <time.h></span> <span class="hljs-comment">#include "sort.h"</span>  void GetLargeData(RecType <span class="hljs-variable">*&</span>R, <span class="hljs-keyword">int</span> n) {     <span class="hljs-keyword">srand</span>(<span class="hljs-keyword">time</span>(<span class="hljs-number">0</span>));     R=(RecType<span class="hljs-variable">*)</span>malloc(sizeof(RecType)<span class="hljs-variable">*n</span>);     <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>; i<n; i++)         R[i].key= <span class="hljs-keyword">rand</span>() % MaxNum;     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"生成了<span class="hljs-variable">%d</span>条记录/n"</span>, n); }  //调用某一排序算法完成排序,返回排序用时 long Sort(RecType <span class="hljs-variable">*&</span>R, <span class="hljs-keyword">int</span> n, void f(RecType<span class="hljs-variable">*,</span> <span class="hljs-keyword">int</span>)) {     <span class="hljs-keyword">int</span> i;     long beginTime, endTime;     RecType <span class="hljs-variable">*R1</span>=(RecType<span class="hljs-variable">*)</span>malloc(sizeof(RecType)<span class="hljs-variable">*n</span>);     <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>;i<n;i++)         R1[i]=R[i];     beginTime = <span class="hljs-keyword">time</span>(<span class="hljs-number">0</span>);     f(R1,n);     endTime = <span class="hljs-keyword">time</span>(<span class="hljs-number">0</span>);     free(R1);     <span class="hljs-keyword">return</span> endTime-beginTime; }  //调用基数排序算法完成排序,返回排序用时 long Sort1(RecType <span class="hljs-variable">*&</span>R, <span class="hljs-keyword">int</span> n) {     long beginTime, endTime;     RadixRecType <span class="hljs-variable">*p</span>;     CreateLink(p,R,n);     beginTime = <span class="hljs-keyword">time</span>(<span class="hljs-number">0</span>);     RadixSort(p);     endTime = <span class="hljs-keyword">time</span>(<span class="hljs-number">0</span>);     DestoryLink(p);     <span class="hljs-keyword">return</span> endTime-beginTime; }  <span class="hljs-keyword">int</span> main() {     RecType <span class="hljs-variable">*R</span>;     <span class="hljs-keyword">int</span> n = MaxSize;   <span class="hljs-regexp">//</span>测试中, MaxSize取<span class="hljs-number">50</span>W     GetLargeData(R, n);     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"各种排序花费时间:/n"</span>);     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"  直接插入排序:<span class="hljs-variable">%ld</span>/n"</span>, Sort(R, n, InsertSort));     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"  希尔排序:<span class="hljs-variable">%ld</span>/n"</span>, Sort(R, n, ShellSort));     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"  冒泡排序:<span class="hljs-variable">%ld</span>/n"</span>, Sort(R, n, BubbleSort));     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"  快速排序:<span class="hljs-variable">%ld</span>/n"</span>, Sort(R, n, QuickSort));     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"  直接选择排序:<span class="hljs-variable">%ld</span>/n"</span>, Sort(R, n, SelectSort));     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"  堆排序:<span class="hljs-variable">%ld</span>/n"</span>, Sort(R, n, HeapSort));     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"  归并排序:<span class="hljs-variable">%ld</span>/n"</span>, Sort(R, n, MergeSort));     <span class="hljs-keyword">printf</span>(<span class="hljs-string">"  基数排序:<span class="hljs-variable">%ld</span>/n"</span>, Sort1(R, n));     free(R);     <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; }

2.头文件 —— sort.h

<span class="hljs-preprocessor">#ifndef SORT_H_INCLUDED</span> <span class="hljs-preprocessor">#define SORT_H_INCLUDED</span>  <span class="hljs-preprocessor">#define MaxSize 50000      <span class="hljs-comment">//最多的数据,取5万,只测试快速算法,可以往大调整</span></span> <span class="hljs-preprocessor">#define MaxNum  2147483647  <span class="hljs-comment">//最大的关键字,取int型的最大值</span></span>  <span class="hljs-comment">//下面的符号常量和结构体针对基数排序</span> <span class="hljs-preprocessor">#define Radix 10           <span class="hljs-comment">//基数的取值</span></span> <span class="hljs-preprocessor">#define Digits 10          <span class="hljs-comment">//关键字位数</span></span>  <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">int</span> KeyType;    <span class="hljs-comment">//定义关键字类型</span> <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">char</span> InfoType[<span class="hljs-number">10</span>]; <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span>          <span class="hljs-comment">//记录类型</span> {     KeyType key;        <span class="hljs-comment">//关键字项</span>     InfoType data;      <span class="hljs-comment">//其他数据项,类型为InfoType</span> } RecType;              <span class="hljs-comment">//排序的记录类型定义</span>  <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> node {     KeyType data;      <span class="hljs-comment">//记录的关键字,同算法讲解中有差别</span>     <span class="hljs-keyword">struct</span> node *next; } RadixRecType;  <span class="hljs-keyword">void</span> InsertSort(RecType R[],<span class="hljs-keyword">int</span> n); <span class="hljs-comment">//直接插入排序</span> <span class="hljs-keyword">void</span> ShellSort(RecType R[],<span class="hljs-keyword">int</span> n);  <span class="hljs-comment">//希尔排序算法</span> <span class="hljs-keyword">void</span> BubbleSort(RecType R[],<span class="hljs-keyword">int</span> n); <span class="hljs-comment">//冒泡排序</span> <span class="hljs-keyword">void</span> QuickSort(RecType R[],<span class="hljs-keyword">int</span> n);  <span class="hljs-comment">//快速排序</span> <span class="hljs-keyword">void</span> SelectSort(RecType R[],<span class="hljs-keyword">int</span> n);  <span class="hljs-comment">//直接选择排序</span> <span class="hljs-keyword">void</span> HeapSort(RecType R[],<span class="hljs-keyword">int</span> n);  <span class="hljs-comment">//堆排序</span> <span class="hljs-keyword">void</span> MergeSort(RecType R[],<span class="hljs-keyword">int</span> n); <span class="hljs-comment">//归并排序</span>  <span class="hljs-comment">//下面函数支持基数排序</span> <span class="hljs-keyword">void</span> CreateLink(RadixRecType *&p,RecType R[],<span class="hljs-keyword">int</span> n);   <span class="hljs-comment">//创建基数排序用的链表</span> <span class="hljs-keyword">void</span> DestoryLink(RadixRecType *&p); <span class="hljs-comment">//释放基数排序用的链表</span> <span class="hljs-keyword">void</span> RadixSort(RadixRecType *&p); <span class="hljs-comment">//基数排序</span>   <span class="hljs-preprocessor">#endif <span class="hljs-comment">// SORT_H_INCLUDED</span></span> 

3.算法的实现—— sort.cpp

<span class="hljs-preprocessor">#include "sort.h"</span> <span class="hljs-preprocessor">#include <malloc.h></span>  <span class="hljs-comment">//1. 对R[0..n-1]按递增有序进行直接插入排序</span> <span class="hljs-keyword">void</span> InsertSort(RecType R[],<span class="hljs-keyword">int</span> n) {     <span class="hljs-keyword">int</span> i,j;     RecType tmp;     <span class="hljs-keyword">for</span> (i=<span class="hljs-number">1</span>; i<n; i++)     {         tmp=R[i];         j=i-<span class="hljs-number">1</span>;            <span class="hljs-comment">//从右向左在有序区R[0..i-1]中找R[i]的插入位置</span>         <span class="hljs-keyword">while</span> (j>=<span class="hljs-number">0</span> && tmp.key<R[j].key)         {             R[j+<span class="hljs-number">1</span>]=R[j]; <span class="hljs-comment">//将关键字大于R[i].key的记录后移</span>             j--;         }         R[j+<span class="hljs-number">1</span>]=tmp;      <span class="hljs-comment">//在j+1处插入R[i]</span>     } }   <span class="hljs-comment">//2. 希尔排序算法</span> <span class="hljs-keyword">void</span> ShellSort(RecType R[],<span class="hljs-keyword">int</span> n) {     <span class="hljs-keyword">int</span> i,j,gap;     RecType tmp;     gap=n/<span class="hljs-number">2</span>;                <span class="hljs-comment">//增量置初值</span>     <span class="hljs-keyword">while</span> (gap><span class="hljs-number">0</span>)     {         <span class="hljs-keyword">for</span> (i=gap; i<n; i++) <span class="hljs-comment">//对所有相隔gap位置的所有元素组进行排序</span>         {             tmp=R[i];             j=i-gap;             <span class="hljs-keyword">while</span> (j>=<span class="hljs-number">0</span> && tmp.key<R[j].key)<span class="hljs-comment">//对相隔gap位置的元素组进行排序</span>             {                 R[j+gap]=R[j];                 j=j-gap;             }             R[j+gap]=tmp;             j=j-gap;         }         gap=gap/<span class="hljs-number">2</span>;  <span class="hljs-comment">//减小增量</span>     } }  <span class="hljs-comment">//3. 冒泡排序</span> <span class="hljs-keyword">void</span> BubbleSort(RecType R[],<span class="hljs-keyword">int</span> n) {     <span class="hljs-keyword">int</span> i,j,exchange;     RecType tmp;     <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i<n-<span class="hljs-number">1</span>; i++)     {         exchange=<span class="hljs-number">0</span>;         <span class="hljs-keyword">for</span> (j=n-<span class="hljs-number">1</span>; j>i; j--)   <span class="hljs-comment">//比较,找出最小关键字的记录</span>             <span class="hljs-keyword">if</span> (R[j].key<R[j-<span class="hljs-number">1</span>].key)             {                 tmp=R[j];  <span class="hljs-comment">//R[j]与R[j-1]进行交换,将最小关键字记录前移</span>                 R[j]=R[j-<span class="hljs-number">1</span>];                 R[j-<span class="hljs-number">1</span>]=tmp;                 exchange=<span class="hljs-number">1</span>;             }         <span class="hljs-keyword">if</span> (exchange==<span class="hljs-number">0</span>)    <span class="hljs-comment">//没有交换,即结束算法</span>             <span class="hljs-keyword">return</span>;     } }  <span class="hljs-comment">//4. 对R[s]至R[t]的元素进行快速排序</span> <span class="hljs-keyword">void</span> QuickSortR(RecType R[],<span class="hljs-keyword">int</span> s,<span class="hljs-keyword">int</span> t) {     <span class="hljs-keyword">int</span> i=s,j=t;     RecType tmp;     <span class="hljs-keyword">if</span> (s<t)                <span class="hljs-comment">//区间内至少存在两个元素的情况</span>     {         tmp=R[s];           <span class="hljs-comment">//用区间的第1个记录作为基准</span>         <span class="hljs-keyword">while</span> (i!=j)        <span class="hljs-comment">//从区间两端交替向中间扫描,直至i=j为止</span>         {             <span class="hljs-keyword">while</span> (j>i && R[j].key>=tmp.key)                 j--;        <span class="hljs-comment">//从右向左扫描,找第1个小于tmp.key的R[j]</span>             R[i]=R[j];      <span class="hljs-comment">//找到这样的R[j],R[i]"R[j]交换</span>             <span class="hljs-keyword">while</span> (i<j && R[i].key<=tmp.key)                 i++;        <span class="hljs-comment">//从左向右扫描,找第1个大于tmp.key的记录R[i]</span>             R[j]=R[i];      <span class="hljs-comment">//找到这样的R[i],R[i]"R[j]交换</span>         }         R[i]=tmp;         QuickSortR(R,s,i-<span class="hljs-number">1</span>);     <span class="hljs-comment">//对左区间递归排序</span>         QuickSortR(R,i+<span class="hljs-number">1</span>,t);     <span class="hljs-comment">//对右区间递归排序</span>     } }  <span class="hljs-comment">//4. 快速排序辅助函数,对外同其他算法统一接口,内部调用递归的快速排序</span> <span class="hljs-keyword">void</span> QuickSort(RecType R[],<span class="hljs-keyword">int</span> n) {     QuickSortR(R, <span class="hljs-number">0</span>, n-<span class="hljs-number">1</span>); }  <span class="hljs-comment">//5. 直接选择排序</span> <span class="hljs-keyword">void</span> SelectSort(RecType R[],<span class="hljs-keyword">int</span> n) {     <span class="hljs-keyword">int</span> i,j,k;     RecType temp;     <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i<n-<span class="hljs-number">1</span>; i++)           <span class="hljs-comment">//做第i趟排序</span>     {         k=i;         <span class="hljs-keyword">for</span> (j=i+<span class="hljs-number">1</span>; j<n; j++)   <span class="hljs-comment">//在当前无序区R[i..n-1]中选key最小的R[k]</span>             <span class="hljs-keyword">if</span> (R[j].key<R[k].key)                 k=j;            <span class="hljs-comment">//k记下目前找到的最小关键字所在的位置</span>         <span class="hljs-keyword">if</span> (k!=i)               <span class="hljs-comment">//交换R[i]和R[k]</span>         {             temp=R[i];             R[i]=R[k];             R[k]=temp;         }     } }  <span class="hljs-comment">//6. 堆排序辅助之——调整堆</span> <span class="hljs-keyword">void</span> sift(RecType R[],<span class="hljs-keyword">int</span> low,<span class="hljs-keyword">int</span> high) {     <span class="hljs-keyword">int</span> i=low,j=<span class="hljs-number">2</span>*i;                        <span class="hljs-comment">//R[j]是R[i]的左孩子</span>     RecType temp=R[i];     <span class="hljs-keyword">while</span> (j<=high)     {         <span class="hljs-keyword">if</span> (j<high && R[j].key<R[j+<span class="hljs-number">1</span>].key)  <span class="hljs-comment">//若右孩子较大,把j指向右孩子</span>             j++;                                <span class="hljs-comment">//变为2i+1</span>         <span class="hljs-keyword">if</span> (temp.key<R[j].key)         {             R[i]=R[j];                          <span class="hljs-comment">//将R[j]调整到双亲结点位置上</span>             i=j;                                <span class="hljs-comment">//修改i和j值,以便继续向下筛选</span>             j=<span class="hljs-number">2</span>*i;         }         <span class="hljs-keyword">else</span> <span class="hljs-keyword">break</span>;                             <span class="hljs-comment">//筛选结束</span>     }     R[i]=temp;                                  <span class="hljs-comment">//被筛选结点的值放入最终位置</span> }  <span class="hljs-comment">//6. 堆排序</span> <span class="hljs-keyword">void</span> HeapSort(RecType R[],<span class="hljs-keyword">int</span> n) {     <span class="hljs-keyword">int</span> i;     RecType temp;     <span class="hljs-keyword">for</span> (i=n/<span class="hljs-number">2</span>; i>=<span class="hljs-number">1</span>; i--) <span class="hljs-comment">//循环建立初始堆</span>         sift(R,i,n);     <span class="hljs-keyword">for</span> (i=n; i>=<span class="hljs-number">2</span>; i--) <span class="hljs-comment">//进行n-1次循环,完成推排序</span>     {         temp=R[<span class="hljs-number">1</span>];       <span class="hljs-comment">//将第一个元素同当前区间内R[1]对换</span>         R[<span class="hljs-number">1</span>]=R[i];         R[i]=temp;         sift(R,<span class="hljs-number">1</span>,i-<span class="hljs-number">1</span>);   <span class="hljs-comment">//筛选R[1]结点,得到i-1个结点的堆</span>     } }  <span class="hljs-comment">//7.归并排序辅助1——合并有序表</span> <span class="hljs-keyword">void</span> Merge(RecType R[],<span class="hljs-keyword">int</span> low,<span class="hljs-keyword">int</span> mid,<span class="hljs-keyword">int</span> high) {     RecType *R1;     <span class="hljs-keyword">int</span> i=low,j=mid+<span class="hljs-number">1</span>,k=<span class="hljs-number">0</span>; <span class="hljs-comment">//k是R1的下标,i、j分别为第1、2段的下标</span>     R1=(RecType *)malloc((high-low+<span class="hljs-number">1</span>)*sizeof(RecType));  <span class="hljs-comment">//动态分配空间</span>     <span class="hljs-keyword">while</span> (i<=mid && j<=high)       <span class="hljs-comment">//在第1段和第2段均未扫描完时循环</span>         <span class="hljs-keyword">if</span> (R[i].key<=R[j].key)     <span class="hljs-comment">//将第1段中的记录放入R1中</span>         {             R1[k]=R[i];             i++;             k++;         }         <span class="hljs-keyword">else</span>                            <span class="hljs-comment">//将第2段中的记录放入R1中</span>         {             R1[k]=R[j];             j++;             k++;         }     <span class="hljs-keyword">while</span> (i<=mid)                      <span class="hljs-comment">//将第1段余下部分复制到R1</span>     {         R1[k]=R[i];         i++;         k++;     }     <span class="hljs-keyword">while</span> (j<=high)                 <span class="hljs-comment">//将第2段余下部分复制到R1</span>     {         R1[k]=R[j];         j++;         k++;     }     <span class="hljs-keyword">for</span> (k=<span class="hljs-number">0</span>,i=low; i<=high; k++,i++) <span class="hljs-comment">//将R1复制回R中</span>         R[i]=R1[k]; }  <span class="hljs-comment">//7. 归并排序辅助2——一趟归并</span> <span class="hljs-keyword">void</span> MergePass(RecType R[],<span class="hljs-keyword">int</span> <span class="hljs-built_in">length</span>,<span class="hljs-keyword">int</span> n)    <span class="hljs-comment">//对整个数序进行一趟归并</span> {     <span class="hljs-keyword">int</span> i;     <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i+<span class="hljs-number">2</span>*<span class="hljs-built_in">length</span>-<span class="hljs-number">1</span><n; i=i+<span class="hljs-number">2</span>*<span class="hljs-built_in">length</span>)     <span class="hljs-comment">//归并length长的两相邻子表</span>         Merge(R,i,i+<span class="hljs-built_in">length</span>-<span class="hljs-number">1</span>,i+<span class="hljs-number">2</span>*<span class="hljs-built_in">length</span>-<span class="hljs-number">1</span>);     <span class="hljs-keyword">if</span> (i+<span class="hljs-built_in">length</span>-<span class="hljs-number">1</span><n)                       <span class="hljs-comment">//余下两个子表,后者长度小于length</span>         Merge(R,i,i+<span class="hljs-built_in">length</span>-<span class="hljs-number">1</span>,n-<span class="hljs-number">1</span>);          <span class="hljs-comment">//归并这两个子表</span> }  <span class="hljs-comment">//7. 归并排序</span> <span class="hljs-keyword">void</span> MergeSort(RecType R[],<span class="hljs-keyword">int</span> n)           <span class="hljs-comment">//自底向上的二路归并算法</span> {     <span class="hljs-keyword">int</span> <span class="hljs-built_in">length</span>;     <span class="hljs-keyword">for</span> (<span class="hljs-built_in">length</span>=<span class="hljs-number">1</span>; <span class="hljs-built_in">length</span><n; <span class="hljs-built_in">length</span>=<span class="hljs-number">2</span>*<span class="hljs-built_in">length</span>) <span class="hljs-comment">//进行log2n趟归并</span>         MergePass(R,<span class="hljs-built_in">length</span>,n); }  <span class="hljs-comment">//以下基数排序,为了统一测试有改造</span> <span class="hljs-comment">//8. 基数排序的辅助函数,创建基数排序用的链表</span> <span class="hljs-keyword">void</span> CreateLink(RadixRecType *&p,RecType R[],<span class="hljs-keyword">int</span> n)   <span class="hljs-comment">//采用后插法产生链表</span> {     <span class="hljs-keyword">int</span> i;     RadixRecType *s,*t;     <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i<n; i++)     {         s=(RadixRecType *)malloc(sizeof(RadixRecType));         s->data = R[i].key;         <span class="hljs-keyword">if</span> (i==<span class="hljs-number">0</span>)         {             p=s;             t=s;         }         <span class="hljs-keyword">else</span>         {             t->next=s;             t=s;         }     }     t->next=NULL; }  <span class="hljs-comment">//8. 基数排序的辅助函数,释放基数排序用的链表</span> <span class="hljs-keyword">void</span> DestoryLink(RadixRecType *&p) {     RadixRecType *q;     <span class="hljs-keyword">while</span>(p!=NULL)     {         q=p->next;         free(p);         p=q;     }     <span class="hljs-keyword">return</span>; }  <span class="hljs-comment">//8. 实现基数排序:*p为待排序序列链表指针,基数R和关键字位数D已经作为符号常量定义好</span> <span class="hljs-keyword">void</span> RadixSort(RadixRecType *&p) {     RadixRecType *head[Radix],*tail[Radix],*t; <span class="hljs-comment">//定义各链队的首尾指针</span>     <span class="hljs-keyword">int</span> i,j,k;     unsigned <span class="hljs-keyword">int</span> d1, d2=<span class="hljs-number">1</span>;   <span class="hljs-comment">//用于分离出第i位数字,见下面的注释</span>     <span class="hljs-keyword">for</span> (i=<span class="hljs-number">1</span>; i<=Digits; i++)                  <span class="hljs-comment">//从低位到高位循环</span>     {         <span class="hljs-comment">//分离出倒数第i位数字,先通过对d1=10^i取余,得到其后i位,再通过整除d2=10^(i-1)得到第i位</span>         <span class="hljs-comment">//例如,分离出倒数第1位,即个位数,先对d1=10取余,再整除d2=1</span>         <span class="hljs-comment">//再例如,分离出倒数第2位,即十位数,先对d1=100取余,再整除d2=10</span>         <span class="hljs-comment">//循环之前,d2已经初始化为1,在这一层循环末增加10倍</span>         <span class="hljs-comment">//下面根据d2,得到d1的值</span>         d1=d2*<span class="hljs-number">10</span>;         <span class="hljs-keyword">for</span> (j=<span class="hljs-number">0</span>; j<Radix; j++)                 <span class="hljs-comment">//初始化各链队首、尾指针</span>             head[j]=tail[j]=NULL;         <span class="hljs-keyword">while</span> (p!=NULL)                 <span class="hljs-comment">//对于原链表中每个结点循环</span>         {             k=(p->data�)/d2;           <span class="hljs-comment">//分离出第i位数字k</span>             <span class="hljs-keyword">if</span> (head[k]==NULL)          <span class="hljs-comment">//进行分配</span>             {                 head[k]=p;                 tail[k]=p;             }             <span class="hljs-keyword">else</span>             {                 tail[k]->next=p;                 tail[k]=p;             }             p=p->next;                  <span class="hljs-comment">//取下一个待排序的元素</span>         }         p=NULL;                         <span class="hljs-comment">//重新用p来收集所有结点</span>         <span class="hljs-keyword">for</span> (j=<span class="hljs-number">0</span>; j<Radix; j++)             <span class="hljs-comment">//对于每一个链队循环</span>             <span class="hljs-keyword">if</span> (head[j]!=NULL)          <span class="hljs-comment">//进行收集</span>             {                 <span class="hljs-keyword">if</span> (p==NULL)                 {                     p=head[j];                     t=tail[j];                 }                 <span class="hljs-keyword">else</span>                 {                     t->next=head[j];                     t=tail[j];                 }             }         t->next=NULL;                   <span class="hljs-comment">//最后一个结点的next域置NULL</span>         <span class="hljs-comment">//下面更新用于分离出第i位数字的d2</span>         d2*=<span class="hljs-number">10</span>;     } } 

作者:sxhelijian 发表于2015/12/1 11:46:10 原文链接

阅读:68 评论:0 查看评论

  • *真实姓名:
  • *手机号码:
  • 公司名称:
  • 咨询内容:

CopyRight © 2009 - 2020 All Right Reserved 备案号:闽ICP备15004550号-275

厦门书生企友通科技有限公司 QYT.com 版权所有