Here is my code:
for(int i = 1; i < n - 1; i++) {
printf("%i\n %i\n", i, j);
int j = i + 1;
if(values[j] < values[i]) {
int tmp = j;
j = i;
i = tmp;
}
}
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.
Sign up to join this communityIf you are trying to swap the values, you need to be using values[i] and values[j], not i and j.
Do you mean:
if(values[j] < values[i]) {
int tmp = j;
j = i;
i = tmp;
}
or
if(values[j] < values[i]) {
int tmp = values[j];
...
}
?
And maybe values[j] is never less than values[i]
Try replacing
if(values[j] < values[i])
{
int tmp = j;
j = i;
i = tmp;
}
with
if(values[j]<values[i])
{
int tmp = values[j];
values[j] = values[i];
values[i] = tmp;
}
found the fix!!!!!! it was only going through the numbers 1 time. I fixed it by adding:
for(int a = 0; a < n - 1; a++) {
to the top (and putting a extra } at the end)