c - 在 C 中对数组进行排序,返回排序后的索引

标签 c arrays sorting

我正在使用 https://phoxis.org/2012/07/12/get-sorted-index-orderting-of-an-array/ 中的示例他从一种数组中返回排序索引,即

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

我已经将他/她的代码翻译成一个返回排序索引数组的函数。代码给出了正确答案。

keeping track of the original indices of an array after sorting in C有类似的 react ,但正如@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;
}

最佳答案

不使用全局变量的直接方法如下所示

#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;
}

程序输出为

8 6 4 3 2 

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

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

内存分配没有意义。

关于c - 在 C 中对数组进行排序,返回排序后的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46962975/

相关文章:

Javascript:访问对象内部的数组

android - 将字符串数组转换为字符串

xml - 对排序序列的操作不遵守排序顺序

java - 如果有零则向左移动

c - Microsoft 驱动程序示例中的 DebugTrace

c - mac上有C的图形库吗?

c - 将数组传递给函数会更改其值吗?

c - C中接收分片UDP数据包的问题

Json 多数组的 C# Web API POST 方法

SQL Server 2008 按主键选择约束顺序