简体   繁体   English

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

[英]Sorting an array in C programming

I can't sort this array in the right way and i don't understand why. 我不能以正确的方式排序这个数组,我不明白为什么。 I want to sort the array but keep the original index so that index can match with my index int the array with players and so I can put the name of the player there. 我想对数组进行排序,但保留原始索引,以便索引可以与我的索引在数组中与玩家匹配,因此我可以将玩家的名称放在那里。

I get something like this: 我得到这样的东西:

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.

Can anyone help me with this ? 谁能帮我这个 ?

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

I believe what you want to do is sort one of your arrays and be able to index into it with the same index as the rest? 我相信你想要做的是对你的一个数组进行排序,并能够使用与其余数组相同的索引对其进行索引?

Why dont you store your myriad arrays as 1 array of the below struct and sort that alone? 为什么不将你的无数数组存储为以下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.

This way you can sort by goals and still have the relevant data for the player together. 这样您就可以按目标排序,并且仍然可以将玩家的相关数据放在一起。

In a quick scan, there are two errors that come to mind. 在快速扫描中,会出现两个错误。


Let's look at LocationOfLargest() . 我们来看看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;
}

Likely you meant for this to return index and not j . 你可能意味着返回index而不是j


Secondly, let's look at what you do to swap the data around when things are out of order. 其次,让我们来看看当事情发生故障时你要做什么来交换数据。

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

This function only swaps the order of the score. 此功能仅交换分数的顺序。 It does not also swap the strings that represent the names. 它也不会交换代表名称的字符串。 You'll need to swap the contents of name_and_country_code as well. 您还需要交换name_and_country_code的内容。


And just for the sake of things, here's an example of a solution to this problem using struct as mentioned in the top level comments. 仅仅为了事情,这里是使用顶级注释中提到的struct解决这个问题的一个例子。 It does clean things up quite a bit by making the associations more explicit. 它通过使关联更明确来确实清理了很多事情。

#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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM