2
#include <stdio.h>

void sort(int *ptr, int n) {
    int i,j,tmp;
    for (i=0;i<n;i++)
        for (j=0;j<n;j++)
                if (ptr[i] < ptr[j])
                {
                    tmp=ptr[i];
                    ptr[i]=ptr[j];
                    ptr[j]=tmp;
                }
}
int main() {
    int i,n;
    int *ptr;
    printf("Nr. of elements : 5 \n");
    n=5;

    ptr=(int*)malloc( n * sizeof(int));
    for (i=0;i<n;i++) {
        scanf("%d",&ptr[i]);
    }

    printf("Initial array is : ");
    for (i=0;i<n;i++) {
        printf("%d ",ptr[i]);
    }

    sort(ptr,n);

    printf("Sorted array is : ");
    for (i=0;i<n;i++) {
        printf("%d ",ptr[i]);
    }
    return 0;
}

This is my code. I'm trying to sort a pointer array using a function. Whatever the (int) input, it sorts out fine. My confusion is that i'm using ptr[i] < ptr[j] instead of ptr[i] > ptr[j] as it should normally be to sort it ascending. Why is that?

1
  • 1
    Just an aside on methods to ask better questions. You could probably minimize the amount of code you needed to share by hard-coding a particular array. Then there wouldn't be any user input necessary. Commented Jan 18, 2020 at 16:58

3 Answers 3

4

No, your confusion is misplaced. Look at the for loops, and the relation between i and j. There are times when i < j and times when i > j, so what constitutes being "out of order" and requiring a swap?

The inner loop should start at i+1 not at '0'; that will make the relation between i and j invariant.

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

Comments

4

Given your loops go from i = 0 .. n and j = 0 .. n, there is no guarantee in your code that i < j.

There's two ways to fix this:

void sort(int *ptr, int n) {
  int i,j,tmp;
  for (i=0; i<n; i++) {
    for (j=0; j<n; j++) {
      if (i < j && ptr[i] < ptr[j]) { // Note the changed conditional
        tmp=ptr[i];
        ptr[i]=ptr[j];
        ptr[j]=tmp;
      }
    }
  }
}

or

void sort(int *ptr, int n) {
  int i,j,tmp;
  for (i=0; i<n; i++) {
    for (j=i+1; j<n; j++) { // Note the changed start value
      if (ptr[i] < ptr[j]) {
        tmp=ptr[i];
        ptr[i]=ptr[j];
        ptr[j]=tmp;
      }
    }
  }
}

1 Comment

... because, in case it is unclear to the OP, the state we want to establish is that for each pair of elements, the value of the one at the lesser index does not exceed the value of the one at the greater index. That's a definition of being sorted (in ascending order). It does not make sense, and in fact it is counterproductive, to perform or act on comparisons that are not relevant to that objective.
-1

As we can see, you are using bubble sort. In bubble sort, our main intention is either transfer the heavier element to the end or lighter element to the top.

(ptr[i] < ptr[j])

what you are doing is moving the heavy elements to the end of the array, this is why whenever you are finding ptr[j](j is an inner loop variable) which is bigger than the ptr[i] (outer loop variable), you are doing a swap.

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.