C 使用qsort排序后存储原始索引

C 使用qsort排序后存储原始索引,c,arrays,sorting,qsort,C,Arrays,Sorting,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} typedef struct { // whatever makes sense to store here, names, stats etc

假设我有以下数组:

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} 
typedef struct
{
  // whatever makes sense to store here, names, stats etc      
} player_t;

player_t players [] = 
{
  {0, ...},
  {1, ...},
};
我还想追踪玩家之前的索引,这样我就可以像这样写排序数组:

A[5]={5,4,3,2,1};
{player 2, player 4, player 1, player 0,player 4} 
typedef struct
{
  // whatever makes sense to store here, names, stats etc      
} player_t;

player_t players [] = 
{
  {0, ...},
  {1, ...},
};
总之,我想追踪原始索引。我用C编写了一个程序,使用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;
}
#包括
#包括
int A[]={2,3,5,4,1};
整数比较(常数无效*a,常数无效*b)
{
返回(*(int*)b-*(int*)a);
}
int main()
{
int n;
qsort(A,5,sizeof(int),比较);

对于(n=0;n,您需要对数字对进行排序,例如,通过在结构中存储每条信息:

struct player {
  int data;
  int index;
};

struct player A[] = {{2,0}, {3,1}, {5,2}, {4,3}, {1,4}};

int compare (const void * a, const void * b)
{
   return ( ((struct player*)b)->data - ((struct player*)a)->data );
}

int main ()
{
  int n;
  qsort (A, 5, sizeof(struct player), compare);
  for (n=0; n<5; n++)
  printf ("data=%d, index=%d\n", A[n].data, A[n].index);
  return 0;
}
struct播放器{
int数据;
整数指数;
};
结构播放器A[]={2,0},{3,1},{5,2},{4,3},{1,4};
整数比较(常数无效*a,常数无效*b)
{
返回(((结构播放器*)b)->数据-((结构播放器*)a)->数据);
}
int main()
{
int n;
qsort(A,5,sizeof(struct player),比较);

对于(n=0;n,您需要对数字对进行排序,例如,通过在结构中存储每条信息:

struct player {
  int data;
  int index;
};

struct player A[] = {{2,0}, {3,1}, {5,2}, {4,3}, {1,4}};

int compare (const void * a, const void * b)
{
   return ( ((struct player*)b)->data - ((struct player*)a)->data );
}

int main ()
{
  int n;
  qsort (A, 5, sizeof(struct player), compare);
  for (n=0; n<5; n++)
  printf ("data=%d, index=%d\n", A[n].data, A[n].index);
  return 0;
}
struct播放器{
int数据;
整数指数;
};
结构播放器A[]={2,0},{3,1},{5,2},{4,3},{1,4};
整数比较(常数无效*a,常数无效*b)
{
返回(((结构播放器*)b)->数据-((结构播放器*)a)->数据);
}
int main()
{
int n;
qsort(A,5,sizeof(struct player),比较);

对于(n=0;n而言,这是一个常见的要求。一种可能的方法是将INT数组替换为2个元素结构的and数组:

struct PlayerScore {
    int playerId;
    int score;
}
您的代码可能会变成:

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

int A[] = {2,3,5,4,1};
#define N sizeof(A)/sizeof(A[0])

struct PlayerScore {
    int playerId;
    int score;
};
int compare (const void * a, const void * b)
{
    return ( (*(struct PlayerScore*)b).score - (*(struct PlayerScore*)a).score );
}

int main ()
{
  int n;
  struct PlayerScore ps[N];
  for(n=0;n<N; n++) {
      ps[n].playerId = n;
      ps[n].score = A[n];
  }
  qsort (ps, 5, sizeof(struct PlayerScore), compare);
  for (n=0; n<N; n++)
      printf ("%d (%d) ",ps[n].score, ps[n].playerId);
  return 0;
}

这是一个常见要求。一种可能的方法是将INT数组替换为2个元素结构的and数组:

struct PlayerScore {
    int playerId;
    int score;
}
您的代码可能会变成:

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

int A[] = {2,3,5,4,1};
#define N sizeof(A)/sizeof(A[0])

struct PlayerScore {
    int playerId;
    int score;
};
int compare (const void * a, const void * b)
{
    return ( (*(struct PlayerScore*)b).score - (*(struct PlayerScore*)a).score );
}

int main ()
{
  int n;
  struct PlayerScore ps[N];
  for(n=0;n<N; n++) {
      ps[n].playerId = n;
      ps[n].score = A[n];
  }
  qsort (ps, 5, sizeof(struct PlayerScore), compare);
  for (n=0; n<N; n++)
      printf ("%d (%d) ",ps[n].score, ps[n].playerId);
  return 0;
}

您的问题与qsort本身无关,只与程序设计有关

“在这个数组中,每个索引代表一个玩家”是个问题。你在数组索引和内容之间创造了一种奇怪的依赖关系,即使你想更改数组索引

用有意义的数据创建一个结构数组,而不是填充有神秘“幻数”的int数组

例如,它可以是这样的:

A[5]={5,4,3,2,1};
{player 2, player 4, player 1, player 0,player 4} 
typedef struct
{
  // whatever makes sense to store here, names, stats etc      
} player_t;

player_t players [] = 
{
  {0, ...},
  {1, ...},
};
现在,您可以根据自己的喜好对该表进行qsort排序


请注意,出于性能原因,最好先声明指向struct的指针数组,然后对该数组进行qsort排序。这样一来,qsort的数据洗牌就更少了。

您的问题与qsort本身无关,而只是程序设计

“在这个数组中,每个索引代表一个玩家”是个问题。你在数组索引和内容之间创造了一种奇怪的依赖关系,即使你想更改数组索引

用有意义的数据创建一个结构数组,而不是填充有神秘“幻数”的int数组

例如,它可以是这样的:

A[5]={5,4,3,2,1};
{player 2, player 4, player 1, player 0,player 4} 
typedef struct
{
  // whatever makes sense to store here, names, stats etc      
} player_t;

player_t players [] = 
{
  {0, ...},
  {1, ...},
};
现在,您可以根据自己的喜好对该表进行qsort排序


请注意,出于性能原因,最好先声明一个指向struct的指针数组,然后对该数组进行qsort排序。这样做可以大大减少qsort的数据混乱。

注意
b-a
按降序排序。是的。我想要降序@Maxim Egorushkin注意
b-a
按降序排序。是的。我想要descending order@Maxim EgorushkinYou没有回答这个问题。@vz0我是,我要指出的是,这应该完全重新设计。我不会通过假装他们的问题有意义,并通过发明一些混乱的魔法数字来误导他们,从而使他们完全困惑,从而帮OP a一个忙。这基本上与发布的答案相同Serge,虽然他的例子可能更像是一个教育学的例子。你没有回答这个问题。@vz0我是,我要指出的是这应该完全重新设计。我不会假装他们的问题是有道理的,并通过发明一些混乱的魔法数字来引导他们完全混淆。这是基本上与Serge的回答相同,尽管他可能更善于用他的好例子进行教学。