• C

Program to order a list of numbers...

I'm doing this program to order a list of 4 given numbers(1,0,3,2) from the smallest to the biggest but something is wrong and don't know what. Any idea??

This is it:

#include <stdio.h>
#include <conio.h>

main()
{
int arr[4]={1,0,3,2};
int i;
order(arr,4);
clrscr();
for(i=0;i<4;i++)
{
printf("%d,",arr[i]);
}
}
order(int arry[],int n)
{
int i,j,k;
for(i=1;i<n;i++)
{
for(j=n-1;j>=1;j--)
{
if(arry[j-1]>arry[j])
{
k=arry[j-1];
arry[j+1]=arry[j];
arry[j]=k;
}
}
}
getch();
}
Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
gothic130:

> I'm doing this program to order a list of 4 given numbers(1,0,3,2) from the
> smallest to the biggest but something is wrong and don't know what. Any idea??

Change the body of your "order" function to be something like this:

void order( int arry[], int n )
{
int i,j,k;

for (i=0; i<n-1; i++)
{
for (j=0; j<n-1-i; j++)
{
if (arry[j+1] < arry[j])
{
k = arry[j];
arry[j] = arry[j+1];
arry[j+1] = k;
}
}
}
getch();
}

Hope That Helps,
Dex*
0
Commented:
>something is wrong and don't know what. Any idea??
Ok let us try to analyze your code a bit

order(int arry[],int n)
{
int i,j,k;
for(i=1;i<n;i++)              ========= this loop will run from 1 to 3
{
for(j=n-1;j>=1;j--)      ========= this will run from 3 to 1 ...
{
if(arry[j-1]>arry[j])  ============ inside the loop, you are not using value of i at all !!!!  ... here you would be simply
comparing elements from 3 to 1  with their neighbours
{
k=arry[j-1];
arry[j+1]=arry[j];
arry[j]=k;
}
}
}
getch();
}

given this background, let us try to trace your program

order(int arry[],int n)        ------- order ( {1,0,3,2 } , 4 )
{
int i,j,k;
for(i=1;i<n;i++)                          i = 1
{
for(j=n-1;j>=1;j--)                     j = 3
{
if(arry[j-1]>arry[j])                 arry[3-1] is 3; arry[3] is 2       3 > 2 so its true
{
k=arry[j-1];                            k = arry[3-1] = 3
arry[j+1]=arry[j];                   arry[3+1] is undefined location... you max index is 3... anyway arry[4] = arry[3] = 2
arry[j]=k;                                arry[3] = k =3        .... so by end of iteration one, you have { 1 0 3 3 } and 2 outside boundary
}                                               scroll down for next iteration
}
}
getch();
}

for(j=n-1;j>=1;j--)              j = 2
{
if(arry[j-1]>arry[j])          arry[1] is 0; arry[2] is 3    ; 0 > 3 is false ... lets go to next iteration
{
k=arry[j-1];
arry[j+1]=arry[j];
arry[j]=k;
}

for(j=n-1;j>=1;j--)                j = 1             { 1 0 3 3 }
{
if(arry[j-1]>arry[j])            arry [0] is 1; arry[1] is 0  ; 1 >0 is true
{
k=arry[j-1];                       k = 1
arry[j+1]=arry[j];              arry[2] = 0
arry[j]=k;                           arry [1] = 1 ... so now you have { 1 1 0 3 }
}

as you can see, things are not quite happening as you wanted them to !!! why ?
because you wanted to swap the values in the inner loop ... swapping is done between two places ... and you broke the cardinal rule ... you invloved three array indices j, j+1, j-1
chaging
arry[j+1]=arry[j];
to
arry[j-1]=arry[j];
will make your program work ...

However, it can still be improved ... you are not using the value of i in inner loop at all ...
after each iteration, lowest index is sorted and does not need to be checked again ... so you can work on a subarray with one lesser index per iteration of outer loop ... I would leave this for you to figure out
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:

k=arry[j-1];
arry[j+1]=arry[j];
arry[j]=k;

USE

k=arry[j-1];
arry[j-1]=arry[j];
arry[j]=k;

0
Commented:
Alternatively, use the stdlib qsort function:
--------8<--------
#include <stdio.h>
#include <stdlib.h>

/* Comparison function for qsort, expecting pointers to point to ints. Returns less than zero if the first argument is less than the second and more than zero if it is greater than the second */
int int_compare(const void* p1,const void *p2)
{
return *(int*)p1 - *(int*)p2;
}

int main()
{
int i;
int arr[4]={1,0,3,2};

/* Do the sort, using stdlib's qsort */

qsort(arr,sizeof(arr)/sizeof(int),sizeof(int),int_compare);

/* Display results */

for (i = 0;i < sizeof(arr)/sizeof(int);i++)
printf("%d,",arr[i]);
}
--------8<--------
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.