R.Dubna
R.Dubna

Reputation: 3

Sorting one array and copying the order over to another

I have two arrays side by side, one lists the different teams and the other lists the scores. I am able to sort the order of scores in descending order. Can this order then be used to move the corresponding team to the correct position of the leader board? eg. move the two teams with 100 points (USA and Germany) to the top of the board

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

Upvotes: 0

Views: 54

Answers (3)

David Ranieri
David Ranieri

Reputation: 41046

The obvious way (as pointed out by others) is embedding your arrays into a struct, but if you are forced to use parallel arrays you can build your own function and sort both arrays at once:

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

Output:

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

Upvotes: 0

qua
qua

Reputation: 53

Also arrange your teams array when sorting;

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

Upvotes: 0

unwind
unwind

Reputation: 400029

As mentioned in a comment, the typical solution is to model your data as an array of structures, rather than separate arrays. This makes sense, since the data is associated with each other.

You'd have something like:

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

Then you can use qsort() (or your own sorting code, if that's of interest) to sort entire structure instances, and the all the data will remain together since entire structures are being moved around.

Upvotes: 2

Related Questions