# Tideman sort_pairs function

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
{
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;
}
``````
• did you modify the structure (struct) of pairs? i see you've another identifier, strength, apart from winnier and loser May 19, 2020 at 7:18
• @domster yes, i modified the struct to contain winner, loser and strength. Also I gave up and used qsort function instead. May 20, 2020 at 3:16
• alright i see. Cheers! May 20, 2020 at 4:09