# Trouble with find_min

I'm trying to implement a bubble sort in the find_min function in pset3 runoff. But It looks like it is not returning the correct min. I think it is because candidates[0].votes may not be the lowest eliminated candidate but I'm not sure how to specify further. Does anyone know what I am doing wrong?

``````int find_min(void)
{
for (int n = 0; n < candidate_count; n++)
{
for (int d = 0; d < candidate_count - 1; d++)
{
{
}
}
}
}
return min;
}
``````

First, there's no guarantee that `candidates[0]` isn't eliminated. You'd have to go through the `candidates` array and find the first one that isn't eliminated.
Also, you're messing up the `candidates` array because you're switching the vote totals, but not the names, so once this function is run, you have no idea how many votes a candidate has. Your code might pass `check50` because of how that program works, but your code won't actually work right.
But most importantly, you don't have to sort. You just have to find the minimum. So what you can do is go through the `candidates` array one element at a time, and check if that candidate has fewer votes than the current minimum number of votes, and if they're not eliminated. If so, that's your new minimum while you go through the rest of the array. This is O(n), while your bubble sort is O(n^2)..
• "When you find the min that's not eliminated within a loop how do you then transfer it outside of the loop? You can't move a variable like that, right?" If the variable was declared inside the loop you can't, but if the variable was declared outside the loop you can. Alternately, you could call `return min` from inside the loop, since you just need to return that value. – Llenk Jun 30 '20 at 15:45
• "Also, how do you switch the candidates struct and not just the votes? I'm not sure how to switch each struct." You know how to switch two ints, and you can switch two `candidate` variables the same way. Declare a spare `candidate` variable, and then do the same procedure with assigning the spare to one, and switching things – Llenk Jun 30 '20 at 15:48