1

I am trying to sort an array from least to greatest using pointers instead of array subscripts. I am not sure where the problem is but when i run this code, the values are returned in the same order that they were entered. The find_largest and swap functions both do exactly what they say. The selection_sort function uses a for loop to sort the numbers from right to left (greatest to smallest, right to left). I have been staring at this for a while now and it looks like it should work fine but like i said, for some reason the numbers are returned in the same order they were entered. Here is my code:

#include <stdio.h>

#define N 5                             

void selection_sort(int *a, int n);
int *find_largest(int *a, int n);
void swap(int *p, int *q);

int main(void)
{
  int i;
  int a[N];

  printf("Enter %d numbers to be sorted: ", N);
  for (i = 0; i < N; i++)
    scanf("%d", (a+i));

  selection_sort(a, N);

  printf("In sorted order:");
  for (i = 0; i < N; i++)
    printf(" %d", *(a+i));
  printf("\n");

  return 0;
}

void selection_sort(int *a, int n)
{
        int i = 0;
        int *largest;

        for(i = 0; i < n; i++){
                largest = find_largest(a, n-i);
                swap(largest, a+(n-1-i));
        }

}

int *find_largest(int *a, int n){
        int *p = a;
        int *largest = p;
        for(p = a; p < a+n-1; p++){
                if(*(p+1) > *p){
                        largest = (p + 1);
                }
        }
        return largest;
}


void swap(int *p, int *q){
        int *temp;
        temp = p;
        p = q;
        q = temp;
}
5
  • 3
    On the contrary, your swap() function does not do what it says. In the first place, swapping the values of the function arguments has no effect outside the function. More importantly, however, it's not the pointers you want to swap, but rather the values to which they point. Commented Sep 26, 2016 at 21:51
  • fixed code Commented Sep 26, 2016 at 22:08
  • In swap, you actually want int temp;. Then, fix up the rest of the code. Commented Sep 26, 2016 at 22:08
  • 1
    @BLUEPIXY: Why not put that fixed swap function in an answer here? Commented Sep 26, 2016 at 22:09
  • That did it. Thanks a lot! Commented Sep 26, 2016 at 22:17

2 Answers 2

2

There are two mistakes in your code. One, logical in the find_largest function:

int *find_largest(int *a, int n){
    int *p = a;
    int *largest = p;
    for(p = a; p < a+n-1; p++){
            if(*(p+1) > *largest){     <---- //here you were checking for *(p)
                    largest = (p + 1);
            }
    }
    return largest;

}

the other is with pointers in swap function:

void swap(int *p, int *q){
    int temp;
    temp = *p;
    *p = *q;
    *q = temp;
}
Sign up to request clarification or add additional context in comments.

Comments

0

As John Bollinger mentioned in the comments, swap() does not function correctly - all it does is reassign pointers that quickly go out of scope. Here is a rewrite of that function that does work. Just swap it in and it fits perfectly.

void swap(int *p, int *q){
        int temp;
        temp = *p;
        *p = *q;
        *q = temp;
}

Thanks to John Bollinger.

Comments

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.