sorting in C

Discussion in 'C' started by angad_aks, Feb 15, 2011.

  1. angad_aks

    angad_aks Banned

    Joined:
    Feb 15, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    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;
    	}
      }
     }
    }
    
    
     
  2. rpbear

    rpbear New Member

    Joined:
    Feb 11, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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 [URL=http://www.go4expert.com/articles/bubble-sort-algorithm-absolute-beginners-t27883/]bubble sort[/URL],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;
    			}
      		}
     	}
    }
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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 likes this.
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    And the sole reason it is not among the articles.
     
  5. angad_aks

    angad_aks Banned

    Joined:
    Feb 15, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    will take care of dat sir.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice