I have been trying to implement sort_pairs function using selection sort algorithm. After testing for every case, the sort_pairs is returning right answer but check50 is marking it as wrong.
for (int i = 0; i < pair_count - 1; i++)
{
winner_index = pairs[i].winner;
loser_index = pairs[i].loser;
diff = preferences[winner_index][loser_index];
int max_index = i;
for (int j = i+1; j < pair_count; j++)
{
winner_index1 = pairs[j].winner;
loser_index1 = pairs[j].loser;
int diff2 = preferences[winner_index1][loser_index1];
if (diff2 > diff)
{
max_index = j;
}
}
if (i != max_index)
{
pairs[i].winner = winner_index1;
pairs[i].loser = loser_index1;
pairs[max_index].winner = winner_index;
pairs[max_index].loser = loser_index;
}
}
Here's the code for reference.
Before Sorting
c a 6
b c 5
a b 7
After Sorting
a b 7
c a 6
b c 5
This is what I am getting as an output where a,b,c are candidates and the numbers are preferences.