简体   繁体   English

如何对结构的2D动态数组进行排序

[英]How to sort 2D dynamic array of structs

I am having trouble sorting a 2D dynamic struct array. 我在排序2D动态结构数组时遇到问题。

I have a struct: 我有一个结构:

typedef struct abc
{
    int total;
} abc;

And a dynamic 2D array: 和动态2D数组:

list = (abc**)malloc(listSize * sizeof(abc*));
    for (int i = 0; i < listSize; i++)
    {
        list[i] = (abc*)malloc(listSize2* sizeof(abc));
    }

I want to use a sorting algorithm: 我想使用排序算法:

qsort(list, listSize, sizeof list[0], cmp);

and the compare function for the qsort: 和qsort的比较函数:

int cmp(const void *l, const void *r)
{
    const abc *a = *(const abc **)l;
    const abc *b = *(const abc **)r;

    return a[0].total > b[0].total;

}

But the problem is that although I think it works for a small list (like about 5 ints), it fails to sort properly if the list is a bit bigger. 但问题是虽然我认为它适用于一个小的列表(如大约5个整数),如果列表有点大,它无法正确排序。 What should I do to the cmp() function so it works properly? 我该怎么做cmp()函数才能正常工作?

By the way, I only need to sort list[x][0] since I will be adding more elements later. 顺便说一句,我只需要对list[x][0]进行排序,因为我稍后会添加更多元素。

(I'm basing my sorting code from another Stackoverflow post) (我基于另一个Stackoverflow帖子的排序代码)

Change the compare function to: 将比较功能更改为:

int cmp(const void *l, const void *r)
{
    const abc *a = *(const abc **)l;
    const abc *b = *(const abc **)r;

    return a[0].total - b[0].total;

}

Using qsort the expected compare function should return negative if first value is smaller positive if it is bigger and 0 if the two values are equal. 使用qsort ,如果第一个值小于正值(如果它更大),则预期比较函数应返回负值;如果两个值相等,则返回0。

EDIT: thanks to WhozCraig: if you think you may hit under or overflow you may go for a safer version: 编辑:感谢WhozCraig:如果你认为你可能会遇到或溢出,你可以选择一个更安全的版本:

int cmp(const void *l, const void *r)
{
    const abc *a = *(const abc **)l;
    const abc *b = *(const abc **)r;

    if (a[0].total < b[0].total) {
       return -1;
    } else if (a[0].total > b[0].total) {
       return 1;
    } else {
       return 0;
    }
}

With the following structure: 具有以下结构:

typedef struct abc {
    int total;
} ABC;

Compare function can be simple as: 比较功能可以简单如下:

int cmp(const void *l, const void *r)
{
    const ABC *a = (const ABC *) l;
    const ABC *b = (const ABC *) r;
    if (a->total == b->total) return 0;
    return (a->total < b->total) ? -1 : 1;
}

used for example like: 用例如:

ABC list[][4]  = {{{5},{2},{0},{4}},
                  {{7},{3},{9},{1}},
                  {{8},{6},{5},{7}},
                  {{2},{7},{9},{5}}};

qsort(list, 4 * 4, sizeof(ABC), cmp);

for (int i = 0; i < 4; ++i)
    for (int j = 0; j < 4; ++j)
        printf("%d ",list[i][j].total);

which outputs: 0 1 2 2 3 4 5 5 5 6 7 7 7 8 9 9 . 输出: 0 1 2 2 3 4 5 5 5 6 7 7 7 8 9 9

And in case you want to sort it within rows only, you could do: 如果您只想在行内对其进行排序,您可以这样做:

for (int i = 0; i < 4; ++i)                 
    qsort(list[i], 4, sizeof(ABC), cmp);

which would give you: 0 2 4 5 1 3 7 9 5 6 7 8 2 5 7 9 . 这会给你: 0 2 4 5 1 3 7 9 5 6 7 8 2 5 7 9 In this case (sorting within rows), it doesn't matter whether the list as whole is stored within the single block of memory or not. 在这种情况下(在行内排序),整个list是否存储在单个内存块中并不重要。 Neither it matters if it has been allocated dynamically or not :) 它是否重要,如果它是动态分配:)

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

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