Sign in to follow this  
Xilo

Radix Sort

Recommended Posts

Xilo    122
I have the radix sort implemented in Java. I use a byte sort implementation. What I don't get is why it's so slow. This was for a previous assignment that I've already turned in, but I thought it be handy to use in my own programming and I'd like to get it faster. On my 1.5ghz PowerBook, it can only sort 500,000 numbers in 5 seconds. The code is below. QueueList is just a basic queue data structure that I've made.
public void sort(QueueList numbers) {//{{{
        boolean negative = false;
	boolean update = true;
	int count = 1;

	for (int j = 0; j < count; ++j) {
		while (!numbers.isEmpty()) {
			try {
				Integer num = (Integer) numbers.dequeue();
				int value = num.intValue();
				int index = (value >> j * 8) & 0xFF;
				queue[index].enqueue(num);

				int next = (value >> (j + 1) * 8) & 0xFF;

				if (update && next != 0 && next != 255) {
					++count;
					update = false;
				}

				if (!negative && next == 255) {
					++count;
					negative = true;
				}
			} catch (Underflow e) {
			}
		}

		update = true;

		if (negative && j == count - 1) {
			while (!queue[255].isEmpty()) {
				try {
					numbers.enqueue(queue[255].dequeue());
				} catch (Underflow e) {
				}
			}
		}

		for (int i = 0; i < queue.length; ++i) {
			while(!queue[i].isEmpty()) {
				try {
					numbers.enqueue(queue[i].dequeue());
				} catch (Underflow e) {
				}
			}
		}
	}
}//}}}

Share this post


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

Sign in to follow this