使用双链表的C++基数排序
C++ radix sort using doubly-linked list
我正在编写一个程序,使用基数排序对数字列表进行排序,但我一直陷入我认为的无限循环中。我认为这要么是我的主要排序函数,要么是我计数函数。你知道我做错了什么吗?
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()中。temp上的循环永远不会终止,因为temp永远不会低于零。
radix_sort函数也有问题。listRay[tmp]未初始化。
相关文章:
- 在基数排序中,我得到 munmap_chunk():无效指针和中止(核心转储).为什么?
- C++基数排序的有效方法
- 基数排序函数不适用于基数 2?
- 基数排序需要使用常量值进行修复
- 基数排序256性能
- C++:使用 LSD 基数排序字符串排序崩溃
- 就地快速基数排序实现
- 最高有效位基数排序如何比最低有效位基数分类更有效
- 霍勒里斯的基数排序
- 如何优化间接基数排序?(又名如何优化不可预测的内存访问模式)
- C++平行的基数排序
- 基数排序函数出现问题
- 用于浮点运算的快速、基于秩的基数排序
- 使用int向量的向量对int向量进行基数排序
- 基数排序C++赋值
- 基数排序C++不屏蔽位
- 使用C++进行基数排序
- c++中对基数排序函数的浮点异常
- 使用位操作的c++基数排序
- 用基数排序实现std::sort的重载是否合法?