[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

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();
}
0
gothic130
Asked:
gothic130
2 Solutions
 
DexstarCommented:
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
 
sunnycoderCommented:
>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
 
AjarCommented:

INSTEAD OF

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
 
rstaveleyCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now