0

我正在开发一个程序来使用基数排序对数字列表进行排序,但我一直陷入我认为是无限循环的困境。我认为它要么是我的主要排序功能,要么是我的计数功能。关于我做错了什么的任何想法?

void radix_sort(DLList& list)
{
  DLList lstRay[10];
  int ct = count(list);
  int zeros = 0;
  int tmp = 0;
  int head = 0;

  for(;ct >= 0; ct--)
  {
      while(!list.isEmpty())
      {
        head = list.deleteHead();
        tmp = head / pow(10, zeros);
        tmp = tmp % 10;
        lstRay[tmp].addToTail(head);
      }
      for(int x = 0; x <=9; x++)
      {
        list.append(lstRay[x]);
      }
      zeros++;   
   }      
}

int count(DLList& list)
{
  int ct = 0;
  int ct2 = 0;
  int tmp = 0;

  while(!list.isEmpty())
  {
      ct = ct2;
      tmp = list.deleteHead();
      while(tmp >= 0)
      {
        tmp = tmp/10;
        ct2++;
      }
      if(ct2 < ct)
      {
        ct2 = ct;
      }
    }
    return ct2;
}
4

2 回答 2

0

You aren't clearing out lstRay on each iteration. This means when you loop around again, each lstRay is getting longer and longer, so your list is growing exponentially.

However, I am surprised that it gets that far - it looks like your count function will clear the list.

于 2014-04-09T03:41:49.817 回答
0

无限循环在 count() 内部。temp 上的循环永远不会终止,因为 temp 永远不会低于零。

radix_sort 函数也有问题。listRay[tmp] 未初始化。

于 2014-04-09T05:07:21.500 回答