首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

链式基数排序有关问题

2012-02-05 
链式基数排序问题链式基数排序问题,下面是算法的实现过程。有些地方不是很理解,麻烦高手帮我在程序后面加一

链式基数排序问题
链式基数排序问题,下面是算法的实现过程。
有些地方不是很理解,
麻烦高手帮我在程序后面加一下注释!!!!!
谢谢了。
lnode   Radix_Sort(lnode   head,int   d)
{
lnode   p,q,h,t;
int   i,j,x,radix=1;
  h=(lnode)malloc(10*sizeof(struct   node));
    t=(lnode)malloc(10*sizeof(struct   node));
      for   (i=d;i> =1;i--)
      {
      for   (j=0;j <=9;j++)
      {   h[j].next=NULL;
      t[j].next=NULL;
      }
      p=head;
      while   (p!=NULL)
      {
      x=((p-> k)/radix)%10;
      if   (h[x].next==NULL)
      {
      h[x].next=p;
      t[x].next=p;
      }
      else
      {
      q=t[x].next;
      q-> next=p;
      t[x].next=p;
      }
      p=p-> next;
      }

      j=0;
      while   (h[j].next==NULL)
        j++;
        head=h[j].next;
        q=t[j].next;
        for   (x=j+1;x <=9;x++)
        if   (h[x].next!=NULL)
        {
        q-> next=h[x].next;
        q=t[x].next;
        }
        q-> next=NULL;
        radix*=10;
        printf( "\n---------------------\n ");
        }
        return   head;
}

[解决办法]
lnode Radix_Sort(lnode head,int d)
{
lnode p,q,h,t;
int i,j,x,radix=1;
h=(lnode)malloc(10*sizeof(struct node));
t=(lnode)malloc(10*sizeof(struct node));
for (i=d;i> =1;i--)//从最低位逐位进行排序
{
for (j=0;j <=9;j++)//将0-9这10个桶的链表全部至空
{ h[j].next=NULL;
t[j].next=NULL;
}
p=head;
while (p!=NULL)//将待排序链表的每个元素逐个放入对应的桶中
{
x=((p-> k)/radix)%10;//根据现在元素的第i位数字,放入第i个桶中
if (h[x].next==NULL)
{
h[x].next=p;
t[x].next=p;
}
else
{
q=t[x].next;
q-> next=p;
t[x].next=p;
}
p=p-> next;
}
//以上是基数排序的分配过程,接下来是基数排序的收集过程
//收集方法就是把所有桶中元素,串起来,形成下一遍排序的序列
j=0;
while (h[j].next==NULL)
j++;
head=h[j].next;
q=t[j].next;
for (x=j+1;x <=9;x++)
if (h[x].next!=NULL)
{
q-> next=h[x].next;
q=t[x].next;
}
q-> next=NULL;
radix*=10;
printf( "\n---------------------\n ");
}
return head;
}

大致就是这样了,具体再看看书吧
仔细看看就看懂了:)

[解决办法]
在 网上看看 清华的 《软件技术实验指导书》 里面有讲解

热点排行