## Tuesday, February 23, 2010

I have been implementing Bead Sort on a variety of different hardware in order to get a feel for programming on each of these different platforms. I haven’t actually spent much thought about the actual algorithm, until recently. I realized after thinking for only a couple minutes that I was going about the whole thing very, very incorrectly.

The way I was doing this before was to line up each of the beads in a giant 2-D array in one stage, make the beads fall in another stage, then manipulate the new array into telling me the sorted numbers in a third stage. I realized that I could combine the first two stages, speed up the entire program, and cut down on the amount of code that need exist.

Instead of setting up a 2-D array of booleans ahead of time, and then making them all fall, I could simply make them fall as I put them on the columns. This decreases the code size drastically. The algorithm for the first two stages now simply goes as such:
1. Assign an index number to each column, from 0 to n.
2. Give each column a single int counter variable, which will refer to the number of beads that are stacked up on that pole
3. Have each column go through the numbers to be sorted, and, if the number to be sorted is greater than the index for the current column, increment the column’s count. Note: This can be done in a very tricky way by saying “count += num > index”.
4. Have each column output it’s count to an output array, in the index of its own column index. Call this output array the “counts” array.
That’s it! Much, much simpler code. What’s more, it decreases the complexity from O(m*n) to O(n), where n is the number of numbers to sort, and m is the maximum size of the numbers to sort.
Now, you have that nice little triangle of sorted numbers. How should one go about reading them off the columns? Well, here’s the “aha” moment. The first number in the sorted array is the number of columns with beads in the first position of the column. How many columns have this property? Well, all the columns with counts greater than 0 have this property. Okay, now, the second number in the sorted array is the number of columns with breads in the second position of the column. How many columns have this property? Well, all the columns with counts greater than 1. The next number in the sorted array is the number of columns with counts greater than 2.

Now, this is the exact same logic as used in the first phase of the bead sort algorithm! Now, each column goes through the counts array, and if the value in the counts array is greater than the column index, that column’s count gets incremented. The only difference is now, the values of m and n are switched. On this second pass, each column is going through the count array, which is of size m (the maximum size of a number to sort). The new output array will be of size n (it has to be, because n is the number of numbers to sort, and the new output array is the sorted numbers).

The real interesting part of this algorithm is that you can simply run the parallel part of the algorithm twice, with different arguments. This leads to very small, clean, readable code. It also turns the runtime of the algorithm from O(m*n) to O(max(m, n)).

My cleanest implementation of this code is probably written in CUDA, and can be found here.

#### 1 comment:

1. okay sorry but this is a comment for a really old post of yours.. and not this one. but anyways.. here goes:

ok so this was a long time ago.. but. i was wondering if you would be down for creating a logo for a fictional evil corporation that we are using as part of a subculture for a band i am in?

the corporation is called the "Optima Syndicate" and more specifically, the "Optima Syndicate Laboratories (or labs)" the corporation is similair to aperture science and the umbrella corporation.. it is a "Facade" or "false front" corporation designed to look like it was doing all this human-life-enhancing R&D, but secretly... this place is cooking up something bad.. but even their biggest rival, Pessicon, can't figure out what.

Pessicon is a vigilante organization conspiring against Optima Syndicate Labs and trying to spearhead a brute force approach into finding and exposing what OSL is really finding in their disturbing inner-core facilities.

So... there are two "companies" with equally interesting cases.. and people make a decision on who they will "support" based on whether they are an optimist (Optima Syndicate Labs) or pessimist (Pessicon)

anyways.. think you could make some logos?