Jump to content

I am working on a program that sorts numbers using a radix sort and I think I am running into an infinite loop in one of these blocks. Anyone spot what is 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;}
Edited by alpenwasser
added code tags
Link to comment
https://linustechtips.com/topic/137446-radix-sort-using-doubly-linked-list/
Share on other sites

Link to post
Share on other sites

can you use the code tag please? a good indentation helps understanding the code too

 

anyway you should put some printf here and there to see where the execution gets stuck

 

anyway, i'd say that this looks like an infinite loop

while(tmp >= 0){              tmp = tmp/10;              ct2++;}
Link to post
Share on other sites

@Ciccioo is right....mainly the >= is getting you into trouble, 1/10 = 0 :P  so tmp > 0 should work.

 

I could be wrong, but couldn't you just replace that entire loop with an if statement log10?

if(tmp > 0)    cnt2 += 1+(int)log10(tmp); //Truncates, so 1 to 9 = 1, 10 to 99 = 2, 100 to 999 = 3, etc...

0b10111010 10101101 11110000 00001101

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×