在C编程中对数组进行排序

时间:2013-06-26 05:22:45

标签: c sorting

我不能以正确的方式对这个数组进行排序,我不明白为什么。我想对数组进行排序,但保留原始索引,以便索引可以与我的索引匹配数组中的玩家,所以我可以将玩家的名字放在那里。

我得到这样的东西:

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;
}

2 个答案:

答案 0 :(得分:2)

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

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

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.

通过这种方式,您可以按目标进行排序,并且仍然可以为玩家提供相关数据。

答案 1 :(得分: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");
}