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; } } } }
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; } } } }
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.