Solved

# Program to order a list of numbers...

Posted on 2003-11-13
261 Views
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();
}
0
Question by:gothic130

LVL 19

Assisted Solution

Dexstar earned 100 total points
ID: 9743812
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

LVL 45

Accepted Solution

sunnycoder earned 400 total points
ID: 9745315
>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

LVL 6

Expert Comment

ID: 9745503

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

LVL 17

Expert Comment

ID: 9756979
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

## Featured Post

### Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.