C 排序时保持两个数组的顺序相同
我有一个整数数组,需要对它进行排序,其中包含unix时间。我打算用qsort对它进行排序,这是相当琐碎的。但是,我还有一个“字符串”数组,它需要保持与整数数组相同的顺序 因此整数数组中的位置2将对应于另一个数组中位置2的元素 有没有使用qsort来维持这种关系?这样做C 排序时保持两个数组的顺序相同,c,arrays,sorting,qsort,C,Arrays,Sorting,Qsort,我有一个整数数组,需要对它进行排序,其中包含unix时间。我打算用qsort对它进行排序,这是相当琐碎的。但是,我还有一个“字符串”数组,它需要保持与整数数组相同的顺序 因此整数数组中的位置2将对应于另一个数组中位置2的元素 有没有使用qsort来维持这种关系?这样做 #include <stdlib.h> #include <stdio.h> struct Data { long int time; const char *string; }; in
#include <stdlib.h>
#include <stdio.h>
struct Data
{
long int time;
const char *string;
};
int
datacmp(const void *const x, const void *const y)
{
return ((struct Data *) x)->time - ((struct Data *) y)->time;
}
int
main(void)
{
struct Data array[] = {
{1234, "1234 Text"},
{1034, "1034 Text"},
{1041, "1041 Text"}
};
size_t count;
count = sizeof(array) / sizeof(array[0]);
for (size_t i = 0 ; i < count ; ++i)
{
fprintf(stderr, "Entry %zu:\n\ttime : %ld\n\tstring: %s\n\n",
i, array[i].time, array[i].string);
}
fprintf(stderr, "\n");
qsort(array, count, sizeof(array[0]), datacmp);
fprintf(stderr, "---- Sorted array:\n");
for (size_t i = 0 ; i < count ; ++i)
{
fprintf(stderr, "Entry %zu:\n\ttime : %ld\n\tstring: %s\n\n",
i, array[i].time, array[i].string);
}
return 0;
}
#包括
#包括
结构数据
{
时间长;
常量字符*字符串;
};
int
datacmp(常数无效*常数x,常数无效*常数y)
{
返回((结构数据*)x)->时间-((结构数据*)y)->时间;
}
int
主(空)
{
结构数据数组[]={
{1234,“1234文本”},
{1034,“1034文本”},
{1041,“1041文本”}
};
大小/数量;
count=sizeof(数组)/sizeof(数组[0]);
用于(大小i=0;i
像这样做
#include <stdlib.h>
#include <stdio.h>
struct Data
{
long int time;
const char *string;
};
int
datacmp(const void *const x, const void *const y)
{
return ((struct Data *) x)->time - ((struct Data *) y)->time;
}
int
main(void)
{
struct Data array[] = {
{1234, "1234 Text"},
{1034, "1034 Text"},
{1041, "1041 Text"}
};
size_t count;
count = sizeof(array) / sizeof(array[0]);
for (size_t i = 0 ; i < count ; ++i)
{
fprintf(stderr, "Entry %zu:\n\ttime : %ld\n\tstring: %s\n\n",
i, array[i].time, array[i].string);
}
fprintf(stderr, "\n");
qsort(array, count, sizeof(array[0]), datacmp);
fprintf(stderr, "---- Sorted array:\n");
for (size_t i = 0 ; i < count ; ++i)
{
fprintf(stderr, "Entry %zu:\n\ttime : %ld\n\tstring: %s\n\n",
i, array[i].time, array[i].string);
}
return 0;
}
#包括
#包括
结构数据
{
时间长;
常量字符*字符串;
};
int
datacmp(常数无效*常数x,常数无效*常数y)
{
返回((结构数据*)x)->时间-((结构数据*)y)->时间;
}
int
主(空)
{
结构数据数组[]={
{1234,“1234文本”},
{1034,“1034文本”},
{1041,“1041文本”}
};
大小/数量;
count=sizeof(数组)/sizeof(数组[0]);
用于(大小i=0;i
一种更通用的解决方案,它根据一个数组对2个(或多个)数组进行实际排序,方法是对指向键数组的指针数组进行排序,然后对所有数组重新排序以对其进行排序(它还将指针数组恢复到初始状态)。compare函数只需要知道指针指向的类型。就地重新排序需要O(n)(线性)时间,因为每次移动都会在其最终排序位置放置一个值。在本例中,a[]是整数数组,b[]是指向字符串(char*)的指针数组
int比较(常量无效*pp0,常量无效*pp1)
{
int i0=**(int**)pp0;
int i1=**(int**)pp1;
如果(i0>i1)返回-1;
如果(i0
一种更通用的解决方案,它根据一个数组对2个(或多个)数组进行实际排序,方法是对指向键数组的指针数组进行排序,然后对所有数组重新排序以对其进行排序(它还将指针数组恢复到初始状态)。compare函数只需要知道指针指向的类型。就地重新排序需要O(n)(线性)时间,因为每次移动都会在其最终排序位置放置一个值。在本例中,a[]是整数数组,b[]是指向字符串(char*)的指针数组
int比较(常量无效*pp0,常量无效*pp1)
{
int i0=**(int**)pp0;
int i1=**(int**)pp1;
如果(i0>i1)返回-1;
如果(i0
使用结构
,这样您就可以绑定数据并按所需的结构成员进行排序。使用间接寻址。创建一个索引数组,0,1,…,N-1并对它们进行排序。@iharob我该如何实现这一点?@fogest你是在问什么是结构?@DavidHeffernan这样比较将与整数数组数据进行比较,但它将对索引数组进行排序?因为根据我的理解,我不能用qsort来实现这一点,因为它使用的数据来自要排序的数组。除非我有不同的理解,否则请使用结构
,即