I am working on a program to sort a list of numbers using a radix sort but I keep getting stuck in what I think is an infinite loop. I think that it is either my main sort function or my counting function. Any ideas on what I am doing wrong?
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;
}
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.
The infinite loop is inside count(). The loop on temp never terminates, because temp never gets below zero.
The radix_sort function has a problem too. listRay[tmp] is not initialised.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.