#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct arrayset {
int *values1;
int *values2;
int *values3;
};
void custom_sort(struct arrayset *v, size_t size);
int main() {
struct arrayset work = {
(int []){ 32, 10, 101, 72, 13, 5 },
(int []){ 40, 10, 100, 90, 20, 2 },
(int []){ 16, 14, 93, 2, 37, 39}
};
custom_sort(&work, 6);
for(int i=0;i<6;++i){
printf("%d, %d, %d\n",
work.values1[i], work.values2[i], work.values3[i]);
}
return 0;
}
typedef struct pair {
int key, value;
} Pair;
int cmp(const void *x, const void *y){
int a = ((const Pair*)x)->key;
int b = ((const Pair*)y)->key;
return a < b ? -1 : a > b;
}
void custom_sort(struct arrayset *v, size_t size){
Pair key[size];
for(int i=0;i<size;++i){
key[i].key = v->values1[i];
key[i].value=i;
}
qsort(key, size, sizeof(Pair), cmp);
int v1[size], v2[size], v3[size];
memcpy(v1, v->values1, size*sizeof(int));
memcpy(v2, v->values2, size*sizeof(int));
memcpy(v3, v->values3, size*sizeof(int));
for(int i=0;i<size;++i){
v->values1[i] = v1[key[i].value];
v->values2[i] = v2[key[i].value];
v->values3[i] = v3[key[i].value];
}
}
struct set { int v1, v2, v3; } array[6];
? - cnicutarelement.v1
进行比较)对该数组进行快速排序(qsort
)。最终你将得到按v1
排序的元素,并能够获取与v1
元素相应的v2
和v3
。 - cnicutarC++11
中有效。 - ajay