简体   繁体   English

在c中排序5d数组

[英]sorting 5d array in c

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. 我试图找出如何在C中对多维数据(5维)进行排序。我知道使用5d数组是一个解决方案,通过阅读关于此主题的其他帖子,许多人发现,如果不是完全不道德的,那么美学上令人厌恶,以便引发不断的抛射物呕吐...所以我提前道歉。

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 35个算法
  • 10 length variables 10个长度变量
  • 230 duration vars 230持续时间的变种
  • 27 plasticity vars 27个可塑性变量
  • 400 fungibility vars 400个可替代性变种

In addition to sorting by algorithm, I would like to have the flexibility to sort on any of the 5 dimensions. 除了按算法排序之外,我还希望能够灵活地对5个维度中的任何维度进行排序。

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++). 这将使用VS 2010 C(非C ++)在具有192 gig(非meg)RAM的12个物理/ 24逻辑核心机器上运行。

I am assuming that qsort would be the most efficient sorting option. 我假设qsort将是最有效的排序选项。 I have searched Google and SO extensively for how to do this to no avail. 我已经广泛搜索谷歌和SO如何做到这一点无济于事。 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. 有1d数组的答案,PHP或C#中的多维数组等,但不适用于C ...或者至少我找不到一个。

qsort in cstdlib would work. cstdlib中的qsort会起作用。 The array is Datatype * **data. 该数组是Datatype * **数据。

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. 如果ab,比较器应返回小于零的值。

int myComparator(void *a, void *b){
    Datatype ****c=(Datatype****)a; Datatype ****d=(Datatype****)b
    return algorithmRatingFunction(b)-algorithmRatingFunction(a);
}

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: 拥有比较器后,您可以对数组进行排序:

qsort(data,35,sizeOf(Datatype),myComparator);

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. 然后是效率低下的问题......如果algorithmRatingFunction需要很长时间才能完成(我猜它确实如此),那么你想要计算所有35个算法一次且只计算一次。 What you could do is calculate the scores beforehand: 你能做的就是事先计算得分:

int scores[35];
for(int n=0;n<35;n++)
    scores[n]=algorithmRatingFunction(data[n]);

Then create another ordered integer array: 然后创建另一个有序整数数组:

int ordering[35];
for(int n=0;n<35;n++)
    ordering[n]=n;

So the state of "ordering" corresponds to the order of your data set. 因此,“排序”状态对应于数据集的顺序。 Then, you can create a new comparator: 然后,您可以创建一个新的比较器:

int myFasterComparator(void *a, void *b){
    int c=*(int*)a; int d=*(int*)b
    return scores[c]-scores[d];
}

And call it on ordering: 并在订购时调用它:

qsort(ordering,35,sizeOf(int),myFasterComparator);

Then reconstruct the array using ordering. 然后使用排序重建数组。 like so: 像这样:

Datatype ****ordereddata[35];
for(int n=0;n<35;n++)
    ordereddata[n]=data[ordering[n]];

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. 像发布的dasblinkenlight一样,qsort减少了将5d数组排序为比较两个4d数组的问题。 So instead of sorting each 4d array you just have to compare two 3d arrays, etc. 因此,不必对每个4d数组进行排序,只需要比较两个3d数组等。

I think you really need to refuse vomiting because of 5D effect. 我认为你真的需要因为5D效果而拒绝呕吐。 Make a struct instead: 改为构造一个结构:

typedef struct {
    int algorithm;
    int length;
    int duration;
    int plasticity;
    int fungibility;
    int dataValue;
} AlgorithmTestData;

And then define your test data 1D array: 然后定义您的测试数据1D数组:

AlgorithmTestData algoTestCases[NUMBER_OF_TEST_CASES];

or you can allocate it dynamically if you don't know the size of test cases with malloc . 或者,如果您不知道malloc的测试用例的大小,您可以动态分配它。

Then you will qsort algoTestCases 1D array according to your comparision requirements. 然后你会qsort根据您的要求比较1D algoTestCases阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM