3

I am trying to write an exchange sort in c using pointers, NOT indices;

I do not understand why this code does not work:

void sort(int a[], int n) {
  int *i, *j, *temp;
  int * const end = a + n;

  for(i = a; i < end - 1; i++)
    for(j = i + 1; j < end; j++)
      if(*i > *j) {
        temp = i;
        i = j;
        j = temp;
      }
}

If I write this code in main, a swap works fine:

int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int *i = a;
int *j;
int *temp;

i = a;
j = a + 1;
printf("pre-swap - i: %i | j: %i\n", *i, *j);

temp = i;
i = j;
j = temp;
printf("post-swap: i: %i | j: %i\n", *i, *j);

It successfully swaps the two values.

But it does nothing to the array when I use this code:

sort(a, 10);

Why isn't my sort algorithm working?

4
  • 9
    It does not swap the values, it swaps the pointers. Check a[0] and a[1], they are still the same as before the swap. Commented Jun 19, 2017 at 17:25
  • 3
    temp = i; i = j; j = temp; -> temp = *i; *i= *j; *j=temp; and int temp; Commented Jun 19, 2017 at 17:31
  • 1
    strongly suggest using a debugger and step through you code. Commented Jun 19, 2017 at 17:33
  • 2
    in your in-main example, you said, "It successfully swaps the two values." - no, you swap the pointer values (i.e. the addresses held in the pointer variables are exchanged). The actual values in a[] remain where they were. The only way to change something pointed to by a pointer is via dereference, which you should be familiar with, as you're using it to both compare and print values. Remember: pointers hold addresses. You need to dereference pointers to get to what that point to. Commented Jun 19, 2017 at 17:55

1 Answer 1

3

Here is the correct solution:

void sort(int a[], int n) {
int *i, *j, temp;
int * const end = a + n;

    for(i = a; i < end-1; i++) {
        for(j = i + 1; j < end; j++) {
            if(*i > *j) {
                temp = *i;
                *i = *j;
                *j = temp;
            }
        }
    }
}

What others have commented above is totally correct. You have to use dereferencing. Read more about pointers and also use debugging. If you are not aware then start with printing out all the values.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm happy to say that I got the solution! I understand pointers to arrays much better now. I must dereference(*) a pointer to access its value. Otherwise, I am accessing the memory address stored in the pointer.
@JonathanDewein I am happy that we all were able to help you :) I saw your profile and found that you have worked in various technologies and sciences. It is good that you are working hard to learn C and algorithms now. All the best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.