1
$\begingroup$

So i understand how to use radix-sort in base 10 and utilize mod 10 to go through the numbers. But not sure about 2,8 or 16.

Does it follow the same idea? and i read somewhere that i need to pass the base number to the radix function but not sure why would that be needed? because if i have for example:

int arr[] = {0xAA, 0x2D , 0x4B, 0x5A, 0x322, 0x24, 0x2, 0x42};

it works perfectly. So not sure why i need to pass the base to the function.

$\endgroup$

1 Answer 1

2
$\begingroup$

Radix sort splits data into own digits of given size, you may choose the base in which data would be treated. If you have 64bit integer you may choose size of radix to be any number, say 2 bits, 4bits, 21bits, 32bits etc., but since computer stores digits as binary numbers it makes more sense to stick with power of two.
Also, you would like to omit modulo operator and use bit masks for efficiency purposes, which is not possible for other numbers.

You haven't described your code, but if it assumes 10 as default base, it is perfectly good, your code is in hexadecimal representation, which gets stored as binary and then treated as decimal based number for sorting. This doesn't change your code, just language feature to provide numbers like that (you can input octal number using 0 in front, say 056, but it will not change running time, but changing radix will).

$\endgroup$
2
  • $\begingroup$ So if we assume this code example, geeksforgeeks.org/radix-sort . Would the size of count array be the base number? its now 10 but if its switched to 16 then its in hexadecimal? $\endgroup$ Commented Apr 24, 2019 at 23:39
  • $\begingroup$ Yes, but you have to change much more to get radix 16 in this code. $\endgroup$
    – Evil
    Commented Apr 25, 2019 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.