0

I written a sorting to an ascending order program using pointers in C.Logically this program works an ascending order,but output is descending order.What happened to this program.

This is my code :

#include<stdio.h>
#include<stdlib.h>
main()
{
int n,i,j,t,*ptr,sum=0;
printf("Enter number of elements to sort : ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{ printf("Error occurred memory not allocated \n"); exit(0);  }
printf("Enter the elements of Array \n");
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
}

for(i=0;i<n;i++){

       for(j=0;j<n;j++)
       {

           if( *(ptr+i) > *(ptr+j))
           {
               t = *(ptr+i);

               *(ptr+i) = *(ptr+j);
               *(ptr+j) = t;
           }
       }
}
for(i=0;i<n;i++)
{
printf("\n%d\n",*(ptr+i));
}

free(ptr);
}
4
  • 1
    *(ptr+i) is idiomatically written ptr[i]. Commented Jun 18, 2014 at 10:25
  • 2
    The initialization of the inner loop is also wrong. Check your textbook/lecture notes. Commented Jun 18, 2014 at 10:27
  • Is this supposed to be bubble sort? If yes, you are doing it wrong. You should compare neighbour elements. Commented Jun 18, 2014 at 10:32
  • Looks more like a selection sort. Commented Jun 18, 2014 at 10:38

4 Answers 4

2

Two things

1) For ascending order, change the if statement to

if( *(ptr+i) < *(ptr+j) )

2) You don't need to check the entire array every time. You can start from the position of the previous lowest value.

for(i=0; i<(n-1); i++){
   for(j=i+1; j<n; j++) {
      ...
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

OK let's do it step by step by a testcase that reproduces the "error".

Suppose that the size of the array is 5, and the user input is 5, 3, 4, 2, 0 , so we have after iniliazization:

a[0]=5;
a[1]=3;
a[2]=4;
a[3]=2;
a[4]=0;

1) i = 0 ,j = 0 : (5 is not > 5) so no change
2) i = 0, j = 1 : (5 is > than 3) so a[0] -> 3 , a[1] -> 5
3) i = 0, j = 2 : (3 is not > 4) so no change
4) i = 0, j = 3 : (3 is > than 2) so a[0] -> 2 , a[3] -> 3
5) i = 0, j = 4 : (2 is > than 0) so a[0] -> 0 , a[4] -> 2

Now i=1;

1) i = 1, j = 0 : (5 is > than 0) so a[1] -> 0 , a[0] -> 5

You see, that you write a[0] = 5 again. This is the main error of your algorithm. If bubbleshort is what you want to implement have a better look at the algorithm (and after that check some more effective sorting algorithms like quicksort.

Comments

1

your program is correct but the line:

if( *(ptr+i) > *(ptr+j))

makes the arrangement in descending order, just make that line:

if( *(ptr+i) < *(ptr+j)) 

and the output will be in ascending order.

Comments

0

I think you are trying to implement Bubble Sort. There is a logical problem in the code in second for loop

for(j=0;j<n;j++)

Change it to

for(j=1;j<n-1;j++). 

I think this will fix the issue

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.