使用双链表的C++基数排序

使用双链表的C++基数排序,c++,sorting,radix,C++,Sorting,Radix,我正在编写一个程序,使用基数排序对数字列表进行排序,但我一直陷入我认为是无限循环的状态。我认为它要么是我的主要排序函数,要么是我的计数函数。你知道我做错了什么吗 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--) { whil

我正在编写一个程序,使用基数排序对数字列表进行排序,但我一直陷入我认为是无限循环的状态。我认为它要么是我的主要排序函数,要么是我的计数函数。你知道我做错了什么吗

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;
}
您没有在每次迭代中清除lstRay。这意味着,当您再次循环时,每个lstRay都会越来越长,因此您的列表会呈指数级增长


然而,我感到惊讶的是,它走了这么远——看起来您的count函数将清除列表。

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

基数排序函数也有问题。listRay[tmp]未初始化