假設我有以下數組排序後的原始索引:存儲使用的qsort
int A[5]={2,3,5,4,1};
在該陣列中,每個索引表示一個播放器。例如:
A[0]=player 0
A[1]=player 1
.....
我想在陣列中像這樣的降序排序:
A[5]={5,4,3,2,1};
,也是我想跟蹤球員的一個索引,這樣我可以寫數組排序像這個:
{player 2, player 4, player 1, player 0,player 4}
總之,我想跟蹤原始索引。我用qsort編寫了一個程序,以降序排列元素。
#include <stdio.h>
#include <stdlib.h>
int A[] = {2,3,5,4,1};
int compare (const void * a, const void * b)
{
return (*(int*)b - *(int*)a);
}
int main()
{
int n;
qsort (A, 5, sizeof(int), compare);
for (n=0; n<5; n++)
printf ("%d ",A[n]);
return 0;
}
是否有可能使用此qsort跟蹤原始索引?
請注意'b - a'按降序排列。 –
是的。我要降序@Maxim Egorushkin – AAA