基于C ++中另一个数组的成员对数组进行排序

时间:2022-11-26 20:14:48

my problem is the next (is an easy example to show the problem):

我的问题是下一个(这是一个显示问题的简单示例):

I have:

int* array1;
double* array2. 

array1=new int[10];
array2=new double[10];
array1=filledWithIntegers(random);
array2=filledWithDoubles(random);

//Here I want to sort array1 based on array2 values. I´m trying to use qsort function of stdlib. qsort(array1,6, sizeof(int), compare);

//这里我想基于array2值对array1进行排序。我正在尝试使用stdlib的qsort函数。 qsort(array1,6,sizeof(int),compare);

The point is how to make the compare function for order array1 based on array2.

关键是如何基于array2为order1生成比较函数。

It is not possible to use std library data structures, it must be done directly in the array pointers.

不可能使用std库数据结构,它必须直接在数组指针中完成。

Thanks.

3 个解决方案

#1


5  

Instead of sorting integers of array1, sort their indexes using array2[index] to compare items, and then re-arrange array1 in accordance with the permutation that you get back from the sort.

不是对array1的整数进行排序,而是使用array2 [index]对它们的索引进行排序以比较项目,然后根据从排序中返回的排列重新排列array1。

Here is a quick demo:

这是一个快速演示:

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

int array1[] = {1, 7, 3, 9, 5};
double array2[] = {1.1, 7.7, 3.3, 9.9, 5.5};

int compare (const void * a, const void * b) {
    double diff = array2[*(int*)a] - array2[*(int*)b];
    return  (0 < diff) - (diff < 0);
}

int main(void) {
    int perm[5], i;
    int res[5];
    for (i = 0 ; i != 5 ; i++) {
        perm[i] = i;
    }
    qsort (perm, 5, sizeof(int), compare);
    for (i = 0 ; i != 5 ; i++) {
        res[i] = array1[perm[i]];
    }
    for (i = 0 ; i != 5 ; i++) {
        printf("%d\n", res[i]);
    }
    return 0;
}

#2


2  

yes. You need to group the two arrays into one array of pair, and then define the compare function.

是。您需要将两个数组分组为一个数组,然后定义比较函数。

the compare function could be:

比较功能可以是:

bool compare(const pair<int,double>& t1, const pair<int,double>& t2){
    return (t1.second < t2.second);
}

#3


1  

Well, you just have to use the position of the elements to index the other array in your comparision function (the standard guarantees that the pointer arguments of the comparison function always point into the to be sorted array):

好吧,你只需要使用元素的位置来索引比较函数中的另一个数组(标准保证比较函数的指针参数总是指向要排序的数组):

int compare(const void *a, const void *b)
{
    unsigned int i = (const int*)a - array1;
    unsigned int j = (const int*)b - array1;
    if(array2[i] < array2[j])
        return -1;
    if(array2[i] > array2[j])
        return 1;
    return 0;
}

The disadvantage is, that the comparison function explicitly needs to know the specific arrays, as it cannot take any additional parameters.

缺点是,比较函数明确需要知道特定的数组,因为它不能采用任何其他参数。

I would question the use of qsort anyway, since your question is tagged C++. Although std::sort has the same problem, you can reach much more genericity/abstraction by using a comparison functor that encapsulates the depending arrays.

无论如何我会质疑qsort的用法,因为你的问题被标记为C ++。虽然std :: sort具有相同的问题,但是通过使用封装依赖数组的比较函数,可以实现更多的通用性/抽象。

#1


5  

Instead of sorting integers of array1, sort their indexes using array2[index] to compare items, and then re-arrange array1 in accordance with the permutation that you get back from the sort.

不是对array1的整数进行排序,而是使用array2 [index]对它们的索引进行排序以比较项目,然后根据从排序中返回的排列重新排列array1。

Here is a quick demo:

这是一个快速演示:

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

int array1[] = {1, 7, 3, 9, 5};
double array2[] = {1.1, 7.7, 3.3, 9.9, 5.5};

int compare (const void * a, const void * b) {
    double diff = array2[*(int*)a] - array2[*(int*)b];
    return  (0 < diff) - (diff < 0);
}

int main(void) {
    int perm[5], i;
    int res[5];
    for (i = 0 ; i != 5 ; i++) {
        perm[i] = i;
    }
    qsort (perm, 5, sizeof(int), compare);
    for (i = 0 ; i != 5 ; i++) {
        res[i] = array1[perm[i]];
    }
    for (i = 0 ; i != 5 ; i++) {
        printf("%d\n", res[i]);
    }
    return 0;
}

#2


2  

yes. You need to group the two arrays into one array of pair, and then define the compare function.

是。您需要将两个数组分组为一个数组,然后定义比较函数。

the compare function could be:

比较功能可以是:

bool compare(const pair<int,double>& t1, const pair<int,double>& t2){
    return (t1.second < t2.second);
}

#3


1  

Well, you just have to use the position of the elements to index the other array in your comparision function (the standard guarantees that the pointer arguments of the comparison function always point into the to be sorted array):

好吧,你只需要使用元素的位置来索引比较函数中的另一个数组(标准保证比较函数的指针参数总是指向要排序的数组):

int compare(const void *a, const void *b)
{
    unsigned int i = (const int*)a - array1;
    unsigned int j = (const int*)b - array1;
    if(array2[i] < array2[j])
        return -1;
    if(array2[i] > array2[j])
        return 1;
    return 0;
}

The disadvantage is, that the comparison function explicitly needs to know the specific arrays, as it cannot take any additional parameters.

缺点是,比较函数明确需要知道特定的数组,因为它不能采用任何其他参数。

I would question the use of qsort anyway, since your question is tagged C++. Although std::sort has the same problem, you can reach much more genericity/abstraction by using a comparison functor that encapsulates the depending arrays.

无论如何我会质疑qsort的用法,因为你的问题被标记为C ++。虽然std :: sort具有相同的问题,但是通过使用封装依赖数组的比较函数,可以实现更多的通用性/抽象。