Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   sorting in C (http://www.go4expert.com/forums/sorting-in-c-t24994/)

angad_aks 16Feb2011 02:01

sorting in C
 
Code:

#include<stdio.h>
#include<conio.h>
void bubble(int *,int);
void main()
{
 int a[40],i,j,n;
 clrscr();
 printf("enter the size of the array");
 scanf("%d",&n);
 printf("enter the sequence");
 for(i=0;i<=n-1;i++)
 {
  scanf("%d",&a[i]);
 }
 printf("the sorted array is:");
 bubble(&a[0],n);
 for(i=0;i<=n-1;i++)
 {
  printf("%d\t",a[i]);
 }

 getch();
}

void bubble(int *a,int n)
{
 int i,j,t;
 for(i=1;i<=n-1;i++)
 {
  for(j=0;j<=n-1-i;j++)
  {
  if(a[j]>a[j+1])
        {
        t=a[j];
        a[j]=a[j+1];
        a[j+1]=t;
        }
  }
 }
}


rpbear 16Feb2011 13:18

Re: sorting in C
 
I don't know what's your question is?while i have something to say.
Code:

#include<stdio.h>

void bubble(int *,int);
//shoule declare the main with the following style
//the one which you used is out of date.
int main()
{
        int a[40],i,j,n;
        //I commit the following code for  successful compilation in gcc
        //clrscr(); 
        printf("enter the size of the array");
        scanf("%d",&n);
        printf("enter the sequence");
        for(i=0;i<=n-1;i++)
        {
                  scanf("%d",&a[i]);
        }
        printf("the sorted array is:\n");
        bubble(&a[0],n);
        for(i=0;i<=n-1;i++)
        {
                  printf("%d\t",a[i]);
        }
        //same reason as above shown.
        //getch();
        return 0;
}

//Long time no use bubble sort,do you mean like this?
void bubble(int *a,int n)
{
        int i,j,t;
        for(i=0;i<=n-1;i++)
        {
                  for(j=i+1;j<=n-1;j++)
                  {
                  if(a[i]>a[j])
                        {
                                t=a[j];
                                a[j]=a[i];
                                a[i]=t;
                        }
                  }
        }
}


xpi0t0s 16Feb2011 14:37

Re: sorting in C
 
Agreed; if you're going to post demo code then
(a) it should not use any platform specific extensions;
(b) it should be a lot better commented than the above. The idea of demo code is for people to learn from it, not for you to show off how great you are.

shabbir 16Feb2011 16:39

Re: sorting in C
 
Quote:

Originally Posted by xpi0t0s (Post 79380)
(b) it should be a lot better commented than the above. The idea of demo code is for people to learn from it, not for you to show off how great you are.

And the sole reason it is not among the articles.

angad_aks 16Feb2011 21:28

Re: sorting in C
 
will take care of dat sir.


All times are GMT +5.5. The time now is 06:37.