1

The other day I decided to write an implementation of radix sort in Java. Radix sort is supposed to be O(k*N) but mine ended up being O(k^2*N) because of the process of breaking down each digit to one number. I broke down each digit by modding (%) the preceding digits out and dividing by ten to eliminate the succeeding digits. I asked my professor if there would be a more efficient way of doing this and he said to use bit operators. Now for my questions: Which method would be the fastest at breaking down each number in Java, 1) Method stated above. 2) Convert number to String and use substrings. 3) Use bit operations.

If 3) then how would that work?

2 Answers 2

3

As a hint, try using a radix other than 10, since computers handle binary arithmetic better than decimal.

  • x >>> n is equivalent to x / 2n
  • x & (2n - 1) is equivalent to x % 2n

By the way, Java's >> performs sign extension, which is probably not what you want in this case. Use >>> instead.

Sign up to request clarification or add additional context in comments.

Comments

2

Radix_sort_(Java)

The line of code that does this;

int key = (a[p] & mask) >> rshift;

is the bit manipulation part.

& is the operator to do a bitwise AND and >> is a right-shift.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.