sorting in C

angad_aks's Avatar
Banned
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;
	}
  }
 }
}
0
rpbear's Avatar, Join Date: Feb 2011
Light Poster
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;
			}
  		}
 	}
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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 like this
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by xpi0t0s View Post
(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.
0
angad_aks's Avatar
Banned
will take care of dat sir.