# Pset3 Tideman: Why is my sort_pairs function not working?

I'm green on all the criteria before sort_pair. Here's my code so far:

``````// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
// TODO
// Create array of strengths of victory
int strengths[pair_count];
for (int i = 0; i < pair_count; i++)
{
strengths[i] = preferences[pairs[i].winner][pairs[i].loser];
}

// Bubble sort
int swaps = 1;
while (swaps > 0)
{
swaps = 1;
for (int i = 0; i < pair_count - 1; i++)
{
if (strengths[i] < strengths[i + 1])
{
pair temp = pairs[i];
pairs[i] = pairs[i + 1];
pairs[i + 1] = temp;
swaps++;
}
}
swaps = swaps - 1;
}
}
``````

The code compiles but it doesn't pass check50. I'm not sure what's wrong with this. Could I please get some advice?

I managed to sort this out! The reason why this didn't work was because the program kept looping indefinitely at this line:

``````if (strengths[i] < strengths[i + 1])
``````

When swapping the pairs, the program didn't swap elements in the "strengths" array, causing the loop to proceed indefinitely. I fixed the problem by making the program sort "strengths" as well:

``````        if (strengths[i] < strengths[i + 1])
{
// Swap strengths
int tmpstrength = strengths[i];
strengths[i] = strengths[i + 1];
strengths[i + 1] = tmpstrength;

// Swap pairs
pair temp = pairs[i];
pairs[i] = pairs[i + 1];
pairs[i + 1] = temp;
swaps++;
}
``````