繁体   English   中英

按C中的第一列对动态分配的2D数组进行排序

[英]Sort dynamically allocated 2D array by first column in C

我有一个较大的2D双打数组,我想按第一列进行排序。 阵列较大(2GB)时,需要动态分配内存。

以下是我的破损代码的简化示例,出于示例目的使用随机数。

#include <stdio.h>
#include <stdlib.h>

int Double_Compare_Function ();

int main()
{
    srand(time(0));
    int p = 0; //property index
    int number_properties = 6; // number_properties
    long long n = 0; //node index
    long long number_nodes = 5; //number of nodes

    /* Declare array */
    double **properties_array;
    properties_array = malloc(number_nodes * sizeof(double*));
    for (n=0; n<number_nodes; n++) {
        properties_array[n] = malloc(number_properties * sizeof(double));
    }

    /* Fill array with numbers */
    for (n=0; n<number_nodes; n++) {
        for (p=0; p<number_properties; p++) {
            properties_array[n][p] = rand();
        }
    }

    printf("Initial array...\n");
    for (n=0; n<number_nodes; n++) {
        printf("%lli: ", n);
        for (p=0; p<number_properties; p++){
            printf("%.1f ", properties_array[n][p]);
        }
        printf("\n");
    }

    /* Sort array */
    qsort(properties_array, (int)number_nodes, number_properties*sizeof(double), Double_Compare_Function);

    printf("Sorted array...\n");
    for (n=0; n<number_nodes; n++) {
        printf("%lli: ", n);
        for (p=0; p<number_properties; p++){
            printf("%.1f ", properties_array[n][p]);
        }
        printf("\n");
    }

    return(0);
}

int Double_Compare_Function (const void * a, const void * b) {
    if (*(double*)a > *(double*)b) return 1;
    else if (*(double*)a < *(double*)b) return -1;
    else return 0;
}

产量

程序编译没有错误(除了随机数生成警告)。 它似乎指向了我不想要的内存。

Initial array...
0: 17189.0 13476.0 24803.0 23588.0 9169.0 13351.0
1: 20992.0 15638.0 23138.0 8580.0 32516.0 24064.0
2: 27139.0 23745.0 19237.0 19279.0 19262.0 25303.0
3: 19407.0 24529.0 23675.0 3102.0 23878.0 5831.0
4: 15299.0 3845.0 27278.0 17467.0 28106.0 6918.0
Sorted array...
0: 17189.0 13476.0 24803.0 23588.0 9169.0 13351.0
1: 19262.0 25303.0 13104405306123376000000000000000000000000.0 0.0 32516.0 24064.0
2: 27139.0 23745.0 1361751537953832600000000000000000000000000000000000000000000000000000.0 0.0 20992.0 15638.0
3: 19407.0 24529.0 23675.0 3102.0 23878.0 5831.0
4: 15299.0 3845.0 27278.0 17467.0 28106.0 6918.0

问题已回答。

您的问题是您无法将由指针到指针组成的对象作为数组进行qsort ,因为没有要求分配的地址在内存中是连续的。 相反,您需要声明并分配双精度数组[number_properties]指针,并在一次调用中分配节点和属性,以确保对象在内存中是连续的,例如

    /* Declare array */
    double (*properties_array)[6];
    properties_array = malloc(number_nodes * number_properties * sizeof(double));
    if (!properties_array) {
        perror ("malloc-properties_array");
        return 1;
    }

注意:从技术上讲,这是双精度[VLA_VLA]指针,除非使用整数常量来指定number_properties从声明number_properties的方式尚不清楚,但随后在代码中使用了整数常量。请注意,如果您选择在代码中使用VLA,则该功能自C11起是实现定义的功能)

现在您的qsort比较可以写成:

int Double_Compare_Function (const void * a, const void * b) {
    if (*(double * const *)a > *(double * const *)b) return 1;
    else if (*(double * const *)a < *(double * const *)b) return -1;
    else return 0;
}

使用/输出示例

$ ./bin/doublecmp
Initial array...
0: 2058999144.0 1013160096.0 499880968.0 1375376710.0 1398189150.0 579626176.0
1: 35952305.0 349854458.0 1000340925.0 1397136257.0 2028006902.0 877319625.0
2: 579560718.0 745830077.0 766399485.0 1052819099.0 1279742925.0 80594279.0
3: 390212763.0 603717917.0 1542566382.0 654797188.0 957950686.0 807072250.0
4: 1163825233.0 1748173998.0 261624942.0 152991913.0 269595164.0 2130895736.0
Sorted array...
0: 35952305.0 349854458.0 1000340925.0 1397136257.0 2028006902.0 877319625.0
1: 390212763.0 603717917.0 1542566382.0 654797188.0 957950686.0 807072250.0
2: 579560718.0 745830077.0 766399485.0 1052819099.0 1279742925.0 80594279.0
3: 1163825233.0 1748173998.0 261624942.0 152991913.0 269595164.0 2130895736.0
4: 2058999144.0 1013160096.0 499880968.0 1375376710.0 1398189150.0 579626176.0

速记比较功能

注意,您还可以编写与条件条件结果的单次返回相同的双比较函数,例如

int Double_Compare_Function (const void * a, const void * b)
{
    return (*(double * const *)a > *(double * const *)b) -
           (*(double * const *)a < *(double * const *)b);
}

您需要对指针进行排序。

您也缺少#include <time.h>

在下面,我添加了缺少的include,仅将打印限制在数组的第一个元素上,并将rand()输出限制为% 100 ,因此数字较小。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int Double_Compare_Function(const void *, const void *);

int number_properties = 6; // number_properties

int main()
{
    srand(time(0));
    int p = 0; //property index
    long long n = 0; //node index
    long long number_nodes = 5; //number of nodes

    /* Declare array */
    double **properties_array;
    properties_array = malloc(number_nodes * sizeof(double*));
    for (n=0; n<number_nodes; n++) {
        properties_array[n] = malloc(number_properties * sizeof(double));
    }

    /* Fill array with numbers */
    for (n=0; n<number_nodes; n++) {
        for (p=0; p<number_properties; p++) {
            properties_array[n][p] = rand() % 100;
        }
    }

    printf("Initial array...\n");
    for (n=0; n<number_nodes; n++) {
        printf("%lli: ", n);
        for (p=0; p<1; p++){
            printf("%.1f ", properties_array[n][p]);
        }
        printf("\n");
    }

    /* Sort array */
    qsort(properties_array, number_nodes, sizeof(double*),
    Double_Compare_Function);

    printf("Sorted array...\n");
    for (n=0; n<number_nodes; n++) {
        printf("%lli: ", n);
        for (p=0; p<1; p++){
            printf("%.1f ", properties_array[n][p]);
        }
        printf("\n");
    }

    return(0);
}

int Double_Compare_Function (const void * a, const void * b) {
  const double * const *z = a;
  const double * const *y = b;
  // the z is a pointer to an array of doubles
  // let's get the first element
  const double k = (*z)[0];
  const double m = (*y)[0];
  return k > m ? 1 : k < m ? -1 : 0;
}

Double_Compare_Function接收两个与properties_array类型相同的指针-它是double** 您需要取消引用两次。 由于每个properties_array元素都指向malloc(number_properties * sizeof(double))返回的值,因此我想使用[0]使其变得冗长,我的意思是数组的第一个元素。 (*z)[0]**z相同。

执行示例:

Initial array...
0: 3.0
1: 94.0
2: 35.0
3: 24.0
4: 71.0
Sorted array...
0: 3.0
1: 24.0
2: 35.0
3: 71.0
4: 94.0

比较的实际参数是double** ,而不是double*
(您将获得指向每个元素的指针,而不是元素本身的指针。)

如果要对第一个元素进行排序,请使用以下内容:

int Compare_Function (const void * a, const void * b) {
    double* array_a = *(double**)a;
    double* array_b = *(double**)b;
    return (int) (array_a[0] - array_b[0]);
}

暂无
暂无

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

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