0

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[4] and values[j] was equivalent to values[5]. Since the swap is only swapping values[min], then values[4] in this case 19 is getting swapped with 27 rather than values[5] 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!

0

You're not actually switching anything in the values[] array. Remember that when you call a function like swap, the parameters are passed by value, not by reference. That means that copies of the contents of values[i] and values[min] are copied to swap() and into a and b. swap() will then swap the contents of a and b, but the contents of values[i] and values[min] remain untouched.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • okay so instead of calling on a swap function, I need to create a int tmp inside of the sort function and swap it in there? – jwang1191 Feb 19 '16 at 18:39
  • That would probably work. Or, if you're intent on doing it as a function, you would pass pointers to the data you wish to swap. (It might be a good exercise to figure out how, but since you're only doing the swap in one place, it's more efficient to do it there.) – Cliff B Feb 19 '16 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .