I tested this sort function separately and it sorts alright, but somehow doesn't pass the check50 test. The pairs.strength is the winning strength, recorded previously in add_pairs function. All my other functions passed check50 except this one and I can't figure out what's wrong. Would appreciate any suggestions. Thanks!
// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
for (int i = 0; i < candidate_count -1; i++) //for each first candidate
{
for (int j = i + 1; j < candidate_count; j++) //paired with each second candidate
{
if (preferences[i][j] > preferences[j][i]) //if first candidate is preferred over second candidate
{
pairs[pair_count].winner = i; //add winners and losers
pairs[pair_count].loser = j;
pairs[pair_count].strength = preferences[i][j]; //record strength of win
pair_count++; //count pairs
}
else if (preferences[i][j] < preferences[j][i]) // if second candidate is preferred over first candidate
{
pairs[pair_count].winner = j;
pairs[pair_count].loser = i;
pairs[pair_count].strength = preferences[j][i];
pair_count++;
}
}
}
return;
}
// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
pair first_unsorted;
for (int i = 0; i < pair_count - 1; i++) //for each first pair
{
for (int j = i + 1; j < pair_count; j++) //paired with a second pair
{
if (pairs[j].strength > pairs[i].strength) //if second strength larger than first, swap
{
first_unsorted = pairs[i];
pairs[i] = pairs[j];
pairs[j] = first_unsorted;
}
}
}
return;
}