0

Here is the code for my pair sorting algorithm.

void sort_pairs(void)
{
    int sorted_pairs_added = 0;
    for (int i = voter_count; i > 0; i += -1)
    {
        for (int j = 0; j < pair_count; j++)
        {
            if(preferences[pairs[j].winner][pairs[j].loser] == i)
            {
                sorted_pairs[sorted_pairs_added] = pairs[j];
                sorted_pairs_added += 1;
            }
        }
    }
    
    for (int i = 0; i < pair_count; i++)
    {
        pairs[i] = sorted_pairs[i];
    }
    
    return;
}

Every example I have tested this with appears to work. check50 says everything is correct up to this point. An example I tried:

Candidates: A, B, C, D
Voters: 3

First Voter: A > D > B > C
Second Voter: A > D > C > B
Third Voter: B > A > C > D

This returns the preferences array:

0 2 3 3
1 0 2 1
0 1 0 1
0 2 2 0

This gives sorted pairs

0,3 i.e. A,D (Wins by 3-0)
0,2 i.e. A,C (Wins by 3-0)
0,1 i.e A,B (Wins by 2-1)
1,2 i.e B,C (Wins by 2-1)
3,1 i.e D,B (Wins by 2-1)
3,2 i.e. D,C (Wins by 2-1)

This is the order they are returned in and it seems right to me. Have I misunderstood how they are supposed to be sorted? Is there any other explanation? Thank you.

0

i think your problem in that:

we need strong of wictory for sort, but your code take only preference of one candidate. your code will take case: 5 wins A candidate vs 4 wins B candidate as 5, but it is only 1.

you need something like: (preferences[pairs[j].winner][pairs[j].loser] - preferences[pairs[j].loser][pairs[j].winner]) == i

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .