# Pset3 selection sort only correctly swaps the first value

I have a question about selection sort code I implemented. Specifically, my swap function seems to be swapping only the first value in a list of numbers through each loop. For example, if I have the numbers 27, 13, 29, 19, 7, my code will swap the first 27 with 7. However, the next loop will switch 13 with 7, then 29 with 7 and then 19 with 7. In the end, the 7 is swapped 4 times and the list just becomes, 7, 7, 7, 7, 7.

Here's my code. The printf's are my way of double checking values

``````void swap (int a, int b);

void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
int min; int j;
for (int i = 0; i < (n-1); i ++)
{
min = i;
printf("value of min %i\n", values[i]);
for (j = (i+1); j < n; j ++)
{
if (values[j] < values[min])
{
printf("value of %i is lower than %i\n", values[j], values[min]);
min = j;
}
}
if (min != i)
{
swap (values[i], values[min]);
printf("swapped %i with %i\n", values[min], values[i]);
}
}
return;
}

void swap (int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
``````

I ran debugger and think I know where the issue is (please correct me if I'm wrong). It appears to me that values[j] where the 7 originally is located is not getting swapped with values[i]. Specifically during debugger, I noticed that j is reiterated in the loop so j = 5 each time since j started at (i+i) which is 1. However, min remained a 4 since min started at i which is 0. Therefore, values[min] was equivalent to values and values[j] was equivalent to values. Since the swap is only swapping values[min], then values in this case 19 is getting swapped with 27 rather than values which is 7

I hope my though process makes sense. I would love to know if I'm right or wrong. I've read some online forums and watched the online tutorials and they all seem to code this way for selection sort. Maybe I just made a mistake somewhere and I'm having trouble finding it. Any help will be appreciated!