sorting in C

Banned
16Feb2011,02:01   #1
angad_aks's Avatar
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;
	}
  }
 }
}
Light Poster
16Feb2011,13:18   #2
rpbear's Avatar
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;
			}
  		}
 	}
}
Mentor
16Feb2011,14:37   #3
xpi0t0s's Avatar
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
Go4Expert Founder
16Feb2011,16:39   #4
shabbir's Avatar
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.
Banned
16Feb2011,21:28   #5
angad_aks's Avatar
will take care of dat sir.