Question

I try to write C++ code to do radix sort for integer. After looking at tutorial online, I found that we have to put each integer to the right bucket, start from the least significant figure. My question is, Do I need 10 buckets from 0 to 9 in normal algorithm for radix sort? If I assign those bucket as a linked list ( ex. *list1 ~~~ *list9), will it seems a little bit weird?

Thank you for your time. This is not a homework but just out of curiosity.

Was it helpful?

Solution

I don't think this is a C++ question (although Radix Sort can clearly be implemented in C++) and it probably has nothing to do with linked lists, at least not in the way you seem to think about it it: The sorting happens on a per digit base where you have efficient access to the buckets for each digit. For this, a list wouldn't work too well but a vector would. Inside each bucket you may use a list.

As with respect to the number of buckets you need, the answer is: it depends! You can use any integral base which is bigger than 1 and you would need a corresponding number of buckets. Since computers are particularly good at computing powers of 2 using a power of 2 is probably more efficient than using other bases like 10 although 10 would also work. Oddly enough, Wikipedia's article on Radix Sort uses base 10 - probably to avoid too much confusion about the free choice of bases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top