对一个阵列进行排序并将订单复制到另一个阵列

时间:2017-03-23 10:59:32

标签: c arrays

我有两个并排阵列,一个列出了不同的团队,另一个列出了分数。我能够按降序排列分数顺序。然后可以使用此订单将相应的团队移动到排行榜的正确位置吗?例如。将两支球队的100分(美国和德国)推向董事会的最高层

#include <stdio.h>

int main() 
{
 char teams[18][20]={"England","Ireland","Wales","Scotland","France","Italy","Germany","Uraguay","Belgium","USA","Mexico","Australia","Belize","Denmark","Sweden","Japan","South Africa","Algeria"};
 int points[18]={43,5,77,23,89,0,100,46,94,100,45,55,32,65,11,37,26,78};
 int i;
 int j;
 int a;

 for (i = 0; i < 18; ++i)
 {
        printf("%i  ",i+1);
        printf("%s",teams[i]);
        printf("\t%d\n", points[i]);
 }
 printf("\n");
 for (i = 0; i < 18; ++i)
 {
        for (j = i + 1; j < 18; ++j)
        {
            if (points[i] < points[j])
            {
                a =  points[i];
                points[i] = points[j];
                points[j] = a;
            }
        }
    }
  for (i = 0; i < 18; ++i)
  {
        printf("%i  ",i+1);
        printf("%s",teams[i]);
        printf("\t%d\n", points[i]);
  }
  return 0;
  }

3 个答案:

答案 0 :(得分:2)

正如评论中所提到的,典型的解决方案是将数据建模为结构数组,而不是单独的数组。这是有道理的,因为数据是相互关联的。

你有类似的东西:

struct score {
  const char *name;
  int points;
} scores[] = {
{ "England", 43 },
{ "Ireland", 5 },
/* and so on */
};

然后您可以使用qsort()(或您自己的排序代码,如果感兴趣的话)对整个结构实例进行排序,并且所有数据将保持在一起,因为整个结构都在移动。

答案 1 :(得分:0)

在排序时也安排你的团队数组;

a = points[i];
b = teams[i];
points[i] = points[j];
teams[i] = teams[j];
points[j] = a;
teams[j] = b;

答案 2 :(得分:0)

显而易见的方法(正如其他人所指出的)是将数组嵌入到struct中,但是如果你被迫使用并行数组,你可以构建自己的函数并立即对两个数组进行排序:

#include <stdio.h>

static int comp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

static void swap(int v1[], char *v2[], int a, int b)
{
    int temp1;
    char *temp2;

    temp1 = v1[a];
    v1[a] = v1[b];
    v1[b] = temp1;
    temp2 = v2[a];
    v2[a] = v2[b];
    v2[b] = temp2;
}

static void sort(int v1[], char *v2[], int left, int right, int (*comp)(const void *, const void *))
{
    int i, last;

    if (left >= right) return;
    swap(v1, v2, left, (left + right) / 2);
    last = left;
    for (i = left + 1; i <= right; i++) {
        if (comp(&v1[i], &v1[left]) < 0)
            swap(v1, v2, ++last, i);
    }
    swap(v1, v2, left, last);
    sort(v1, v2, left, last - 1, comp);
    sort(v1, v2, last + 1, right, comp);
}

int main(void) 
{
    char *teams[] = {"England","Ireland","Wales","Scotland","France","Italy","Germany","Uraguay","Belgium","USA","Mexico","Australia","Belize","Denmark","Sweden","Japan","South Africa","Algeria"};
    int points[] = {43,5,77,23,89,0,100,46,94,100,45,55,32,65,11,37,26,78};
    size_t i, n = sizeof(points) / sizeof(*points);

    sort(points, teams, 0, n - 1, comp);
    for (i = 0; i < n; i++) {
        printf("%s->%d\n", teams[i], points[i]);
    }
    return 0;
}

输出:

Italy->0
Ireland->5
Sweden->11
Scotland->23
South Africa->26
Belize->32
Japan->37
England->43
Mexico->45
Uraguay->46
Australia->55
Denmark->65
Wales->77
Algeria->78
France->89
Belgium->94
Germany->100
USA->100