0

我无法以正确的方式对这个数组进行排序,我不明白为什么。我想对数组进行排序,但保留原始索引,以便索引可以与我的索引 int 数组与播放器匹配,因此我可以将播放器的名称放在那里。

我得到这样的东西:

before sorting 
_________________________________
Players ranked are David Beckham  ENG ---- 0.
Players ranked are Wayne Rooney  ENG ---- 5.
Players ranked are Pirlo  ITA ---- 3.
Players ranked are Del Piero  ITA ---- 2.
Players ranked are Lionel Messi  ARG ---- 5.

after sorting ( which is wrong )

Players ranked are David Beckham  ENG ---- 0.
Players ranked are Wayne Rooney  ENG ---- 0.
Players ranked are Pirlo  ITA ---- 5.
Players ranked are Del Piero  ITA ---- 3.
Players ranked are Lionel Messi  ARG ---- 2.

谁能帮我这个 ?

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#define PLAYERS 5
#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20

void sort_func(int array[], char name_and_country_code[][LENGTH_NAME], int elements);
int LocationOfLargest(int array[], int n);
print_array (int array[], char name_and_country_code[][LENGTH_NAME], int elements);
void swap (int *a , int *b);

int main (void)
{
    int match1[PLAYERS] = { 0,1,3,2,4};
    int match2[PLAYERS] = { 0,4,0,0,1};
    int goals[PLAYERS] ;

    char name[PLAYERS][LENGTH_NAME] ={"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
    char country_abbreviations[PLAYERS][LENGTH_CODE] = {"ENG","ENG","ITA","ITA","ARG"};
    char country_code[NUM_COUNTRIES][LENGTH_CODE] = {"ARG","ENG","ITA"};
    char country_name[NUM_COUNTRIES][LENGTH_COUNTRY] = {"Argentina", "England","Italy"};
    char name_and_country_code[PLAYERS][LENGTH_NAME];
    char country_code_and_country_name[NUM_COUNTRIES][LENGTH_COUNTRY];
    int i, first =1, second= 2;

    for(i=0; i < PLAYERS; i++)
    {
        strcpy (name_and_country_code[i], name[i]);
        strcat (name_and_country_code[i], "  " );
        strcat (name_and_country_code[i], country_abbreviations[i]);
        goals[i]= match1[i] + match2[i];
        printf("Player %s----- score %d:\n", name_and_country_code[i], goals[i]);
    }

    printf("\n_________________________________\n");
    //before sorting
    print_array ( goals, name_and_country_code, PLAYERS );
    printf("\n");
    sort_func ( goals, name_and_country_code, PLAYERS);
    //after sorting not working right
    print_array ( goals, name_and_country_code, PLAYERS );

    return 0;
}

print_array (int array[], char name_and_country_code[][LENGTH_NAME], int elements)
{
    int i ;
    for ( i = 0 ; i < PLAYERS; i ++)
    {
        printf ("Players ranked are %s ---- %d.\n", name_and_country_code[i], array[i]);
    }
}

void sort_func(int array[], char name_and_country_code[][LENGTH_NAME], int elements)
{
    int index ,last = elements-1;
    while (last >0 )
    {
        index = LocationOfLargest(array, last);
        swap (&array[last], &array [index]);
        last--;
    }
}

void swap (int *a , int *b)
{
    int tmp = *a ;
    *a = *b;
    *b = tmp;
}

int LocationOfLargest(int array[], int n)
{
    int j , index =0 ;
    for (j = 0 ; j <= n ; j ++)
        if (array[index] < array[j])
            index = j;
    return j;
}
4

2 回答 2

2

我相信您想要做的是对您的一个数组进行排序,并能够使用与其他数组相同的索引对其进行索引?

为什么不将无数数组存储为以下 1 个数组struct并单独排序?

typedef struct{
    int match1, match2, goals;
    char name[LENGTH_NAME];
    //and  so on

} Player;

...

Player players[PLAYERS]; 

//Now sort the array of players according to the goals.

这样,您可以按目标排序,并且仍然可以将玩家的相关数据放在一起。

于 2013-06-26T05:38:19.683 回答
0

在快速扫描中,我想到了两个错误。


让我们看看LocationOfLargest()

int LocationOfLargest(int array[], int n)
{
    int j , index =0 ;
        for (j = 0 ; j <= n ; j ++)
            if (array[index] < array[j])
                index = j;
    return j;
}

可能你的意思是返回index而不是返回j


其次,让我们看看当事情出现问题时,您如何交换数据。

void sort_func(int array[], char name_and_country_code[][LENGTH_NAME], int elements)
{
    int index ,last = elements-1;
    while (last >0 )
    {
        index = LocationOfLargest(array, last);
        swap (&array[last], &array [index]);
        last--;
    }
}

此功能仅交换分数的顺序。它也不交换代表名称的字符串。您还需要交换其中的内容name_and_country_code


struct只是为了方便起见,这里有一个使用顶级评论中提到的解决此问题的示例。通过使关联更加明确,它确实清理了很多东西。

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

#define PLAYERS 5
#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20

static const int match1[PLAYERS] = {0,1,3,2,4};
static const int match2[PLAYERS] = {0,4,0,0,1};
static const char *name[PLAYERS] = {"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
static const char *countries[PLAYERS] = {"ENG","ENG","ITA","ITA","ARG"};

struct Player {
    char name[LENGTH_NAME];
    int score;
};

void swap(struct Player *a , struct Player *b) {
    struct Player tmp = *a;
    *a = *b;
    *b = tmp;
}

int LocationOfLargest(struct Player *array, int n) {
    int i, largest = 0 ;
    for (i = 0 ; i <= n ; ++i)
        if (array[largest].score < array[i].score)
            largest = i;
    return largest;
}

void sort_func(struct Player *array, int size) {
    int last = size - 1;
    while (last > 0) {
        int index = LocationOfLargest(array, last);
        swap(&array[last], &array[index]);
        last--;
    }
}

int main (void) {
    struct Player players[PLAYERS];
    int i;

    // Construct each player object
    for (i=0; i < PLAYERS; ++i) {
        snprintf(players[i].name, LENGTH_NAME, "%s   %s", name[i], countries[i]);
        players[i].score = match1[i] + match2[i];
    }

    // Print before sorting
    printf("Before sorting:\n");
    for (i=0; i < PLAYERS; ++i)
        printf("Player %s ---- score: %d\n", players[i].name, players[i].score);
    printf("\n");

    // Sort the data
    sort_func(players, PLAYERS);

    // Print after sorting
    printf("After sorting:\n");
    for (i=0; i < PLAYERS; ++i)
        printf("Player %s ---- score: %d\n", players[i].name, players[i].score);
    printf("\n");
}
于 2013-06-26T05:55:13.353 回答