Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的排序数组,返回排序后的索引_C_Arrays_Sorting - Fatal编程技术网

C中的排序数组,返回排序后的索引

C中的排序数组,返回排序后的索引,c,arrays,sorting,C,Arrays,Sorting,我使用了一个例子,他从数组的排序返回排序索引,即 3,4,2,6,8返回4,3,1,0,2(+1表示R中的每个索引)。这相当于R的order函数 我已经将他/她的代码转换为返回排序索引数组的函数。代码给出了正确的答案 有类似的反应,但正如@BLUEPIXY警告的那样,他的解决方案并不适用于所有情况。我需要在任何情况下都能用的东西,包括领带 但是,原始作者使用了一个全局指针,这会导致内存泄漏,free()不会修复它。如果没有全局指针,我不知道怎么做 如何修复这个内存泄漏,或者至少返回C中的排序索引

我使用了一个例子,他从数组的排序返回排序索引,即

3,4,2,6,8
返回
4,3,1,0,2
(+1表示R中的每个索引)。这相当于R的
order
函数

我已经将他/她的代码转换为返回排序索引数组的函数。代码给出了正确的答案

有类似的反应,但正如@BLUEPIXY警告的那样,他的解决方案并不适用于所有情况。我需要在任何情况下都能用的东西,包括领带

但是,原始作者使用了一个全局指针,这会导致内存泄漏,
free()
不会修复它。如果没有全局指针,我不知道怎么做

如何修复这个内存泄漏,或者至少返回C中的排序索引,这些索引将始终有效

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

/* holds the address of the array of which the sorted index
 * order needs to be found
 */
int * base_arr = NULL;
/* Note how the compare function compares the values of the
 * array to be sorted. The passed value to this function
 * by `qsort' are actually the `idx' array elements.
 */
static int compar_increase (const void * a, const void * b) {
    int aa = *((int * ) a), bb = *((int *) b);
    if (base_arr[aa] < base_arr[bb]) {
        return 1;
    } else if (base_arr[aa] == base_arr[bb]) {
        return 0;
    } else {
//  if (base_arr[aa] > base_arr[bb])
        return -1;
     }
}

int * order_int (const int * ARRAY, const size_t SIZE) {
    int * idx = malloc(SIZE * sizeof(int));
    base_arr = malloc(sizeof(int) * SIZE);
    for (size_t i = 0; i < SIZE; i++) {
        base_arr[i] = ARRAY[i];
        idx[i] = i;
    }
    qsort(idx, SIZE, sizeof(int), compar_increase);
    free(base_arr); base_arr = NULL;
    return idx;
}


int main () {
    const int a[] = {3,4,2,6,8};
    int * b = malloc(sizeof(int) * sizeof(a) / sizeof (*a));
    b = order_int(a, sizeof(a) / sizeof(*a));
    for (size_t i = 0; i < sizeof(a)/sizeof(*a); i++) {
        printf("b[%lu] = %d\n", i, b[i]+1);
    }
    free(b); b = NULL;
    return 0;
}
#包括
#包括
/*保存已排序索引所在数组的地址
*需要找到秩序
*/
int*base_arr=NULL;
/*注意compare函数如何比较
*要排序的数组。传递给此函数的值
*by`qsort'实际上是`idx'数组元素。
*/
静态整数比较增加(常数无效*a,常数无效*b){
int aa=*((int*)a),bb=*((int*)b);
if(基本阵列[aa]<基本阵列[bb]){
返回1;
}否则,如果(基本阵列[aa]==基本阵列[bb]){
返回0;
}否则{
//if(基本阵列[aa]>基本阵列[bb])
返回-1;
}
}
整数*顺序整数(常数整数*数组,常数大小){
int*idx=malloc(SIZE*sizeof(int));
基本arr=malloc(sizeof(int)*大小);
对于(大小i=0;i
我看到的问题是,在main函数中,您正在为指针b分配一些内存-

   int * b = malloc(sizeof(int) * sizeof(a) / sizeof (*a));
下一行调用order_int(…),返回指向已分配内存的指针-

  b = order_int(a, sizeof(a) / sizeof(*a));
查看order_int函数-

int * order_int (const int * ARRAY, const size_t SIZE) {
    int * idx = malloc(SIZE * sizeof(int));
    base_arr = malloc(sizeof(int) * SIZE);
    for (size_t i = 0; i < SIZE; i++) {
        base_arr[i] = ARRAY[i];
        idx[i] = i;
    }
    qsort(idx, SIZE, sizeof(int), compar_increase);
    free(base_arr); base_arr = NULL;
    return idx;
}

不使用全局变量的简单方法可以如下所示

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

int cmp_ptr(const void *a, const void *b)
{
    const int **left  = (const int **)a;
    const int **right = (const int **)b;

    return (**left < **right) - (**right < **left);
}

size_t * order_int(const int *a, size_t n)
{
    const int **pointers = malloc(n * sizeof(const int *));

    for (size_t i = 0; i < n; i++) pointers[i] = a + i;

    qsort(pointers, n, sizeof(const int *), cmp_ptr);

    size_t *indices = malloc(n * sizeof(size_t));

    for (size_t i = 0; i < n; i++) indices[i] = pointers[i] - a;

    free(pointers);

    return indices;
}

int main( void )
{
    const int a[] = { 3,4,2,6,8 };
    const size_t N = sizeof(a) / sizeof(*a);
    size_t *indices = order_int(a, N);

    for (size_t i = 0; i < N; i++) printf("%d ", a[indices[i]]);
    putchar('\n');

    free(indices);

    return 0;
}
至于内存泄漏,则是由于覆盖了指向冗余分配内存的指针的值

int * b = malloc(sizeof(int) * sizeof(a) / sizeof (*a));
b = order_int(a, sizeof(a) / sizeof(*a));

内存分配没有意义。

回答您的问题的方法太多了。有一条建议是,您不需要为指针b malloc memory,因为order_int(..)返回一个已经分配内存的指针。@Neil,哇,就是这样!你能把你的解决方案写下来吗?这样我就可以给你打绿色的复选标记了?所需要的只是改变
int*b=malloc(sizeof(int)*sizeof(a)/sizeof(*a))
int*b
8 6 4 3 2 
int * b = malloc(sizeof(int) * sizeof(a) / sizeof (*a));
b = order_int(a, sizeof(a) / sizeof(*a));