When working on "The fastest sort for BrainF***", I discovered this algorithm, which is O(N*k), where k is the maximum value in the input. It requires O(N) extra storage.
The physical analogy is that you have N stacks of tokens. The height of the stack represents the value to be sorted. (Each token represents a bit). Set aside space for another N stacks. You take one token off the top of each stack that has tokens, and then add one to each stack in the new set from right to left until your hand is empty. Repeat until all original stacks are empty. Now the new set is sorted ascending left to right
In C:
void sort(int A[], int N)
{
int *R = calloc(N,sizeof(int));
do {
int i,count=0;
for (i=0;i<N;i++) if A[i] { count++; A[i]--;}
for (i=0;i<count;i++) R[i]++;
} while (count);
memcpy(A,R,N); //A is now sorted descending.
free(R);
}
Does this algorithm have a name? It seems similar to a Bead Sort, but I don't think it's quite the same.
Introducing LSD Radix Sort Unlike other sorting algorithms, LSD Radix Sort is non-comparative and only utilizes hashing ( O(1) amortized) and simple traversals ( O(n) ); put these two ingredients together, and you have a worst-case O(n) sorting algorithm!
A sorting algorithm is a method for reorganizing a large number of items into a specific order, such as alphabetical, highest-to-lowest value or shortest-to-longest distance. Sorting algorithms take lists of items as input data, perform specific operations on those lists and deliver ordered arrays as output.
A sorting algorithm has space complexity O(1) by allocating a constant amount of space, such as a few variables for iteration and such, that are not proportional to the size of the input.
Turns out I wasn't too lazy after all. It is Bead Sort. Here's the definition from the original paper (PDF link):
Consider a set A of n positive integers. . . For all a in A drop a beads (one bead per rod) along the rods, starting from the 1st rod to the a'th rod. Finally, the beads, seen level by level, from the nth level to the first level, represent A in ascending order.
This implementation transforms that algorithm in two ways:
y=x
. This changes the result such that the number of 'beads' in each column represents the output sorted in descending order. In the original algorithm, the number of 'beads' in each row represents the output sorted in ascending order.Here's some clarification on that first point, taken straight from the diagram on the paper's second page: As the algorithm is originally implemented, the array [3, 2, 4, 2] would be represented by a grid that looks like:
* * *
* *
* * * *
* *
And letting the beads fall produces:
* *
* *
* * *
* * * *
You then have to read the rows, from top to bottom, to get the output: [2, 2, 3, 4]. Whereas in the version that gives results in descending order, you are effectively doing this instead:
* *
* * * *
* * * * -> * * * *
* * * * * * * *
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With