I am trying to figure out how to sort a multidimensional data (5 dimensions) in C. I know that using a 5d array is a solution that, from
reading others posts on SO about this topic many folks find, if not altogether unethical, so aesthetically repugnant so as to provoke unceasing projectile vomiting…so I apologize in advance.
Essentially I have an incoming set of data to which I must apply a series of discrete algorithms. Each algorithm has a set of variables, and I need to calculate a ranking of the efficiency of each algorithm with every permutation of the
variables possible.
Ultimately, I need a list sorted by the best-to-worst performing algorithm. The whole
calculation is dynamic, so what works best on one incoming piece of data is unlikely to be the best performer on another…so I can’t eliminate any of the variables because they are poor performers.
Here is how the data looks:
dataValue[ algo ][ lengthVar ][ durationVar ][ plasticityVar ] [ fungibilityVar]
There are:
- 35 algorithms
- 10 length variables
- 230 duration vars
- 27 plasticity vars
- 400 fungibility vars
In addition to sorting by algorithm, I would like to have the flexibility to sort on any of the 5 dimensions.
This will be run on a 12 physical/ 24 logical core machine with 192 gig (not meg) of RAM, using VS 2010 C (not C++).
I am assuming that qsort would be the most efficient sorting option. I have searched Google and SO extensively for how to do this to no avail. There are answers for 1d arrays, multidimensional arrays in PHP or C#, etc, but not for C…or at least I can’t find one.
qsort in cstdlib would work. The array is Datatype ***data.
So first off, lets say that you want to sort the first index of the array. You’d have to write a comparator function to compare two Datatype****s. The comparator should return a value less than zero if ab.
This is pretty obviously inefficient because you have to reevaluate the algorithm for each dataset each comparison, but lets get to that in a sec. After you have the comparator, you can sort the array:
That’s it!
Then there’s the issue of inefficiency… If algorithmRatingFunction takes a long time to complete (which I’m guessing it does), then you’d want to calculate all 35 algorithms once and only once. What you could do is calculate the scores beforehand:
Then create another ordered integer array:
So the state of “ordering” corresponds to the order of your data set. Then, you can create a new comparator:
And call it on ordering:
Then reconstruct the array using ordering. like so:
The same holds true for all other levels. Like dasblinkenlight posted, qsort reduces the problem of sorting 5d arrays to that of comparing two 4d arrays. So instead of sorting each 4d array you just have to compare two 3d arrays, etc.