Practical 5B- Wap for bubble sort

Posted: January 20, 2012 in Lab 3
#include<stdio.h>  
#include<conio.h>  
  
  void bubble(int a[],int n)  
  {  
        int i,j,t;  
         for(i=n-2;i>=0;i--)  
         {  
            for(j=0;j<=i;j++)  
  
                  {  
                    if(a[j]>a[j+1])  
                                    {  
                                      t=a[j];  
                                     a[j]=a[j+1];  
                                     a[j+1]=t;  
                                    }  
                   }  
         
  
           }  
  
  }
  
  
  void main()  
  {  
  
      int a[100],n,i;  
  
  
      printf("\n\n Enter integer value for total no.s of elements to be sorted: ");  
      scanf("%d",&n);  
	printf("\n\n Enter integer value for element no: ");
  
      for( i=0;i<=n-1;i++)  
            {   
              scanf("%d",&a[i]);  
            }  
  
       bubble(a,n);  
  
       printf("\n\n Finally sorted array is: ");  
       for( i=0;i<=n-1;i++)  
       printf("%3d",a[i]);  
  
  } 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s