Search This Blog

Friday, July 8, 2011

Sorting Algorithm_BUBBLE

/*Here is the program for bubble sorting..Enjoy Friends...!!!*/
#include<conio.h>
#include<stdio.h>
#define n 5
void main()
{
 int i,j,a[100],temp,count=0;
 clrscr();
 printf("Enter the elements\n");
 for(i=0;i<n;i++)
 {
  scanf("%d\n",&a[i]);
 }
 for(i=0;i<n;i++)
 {
  for(j=i;j<n;j++)
  {
   if(a[i]>a[j])
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    count++;                            //for counting complexity..
   }
  }
 }
 printf("\nAfter Sorting\n");
 for(i=0;i<n;i++)
 {
  printf("%d\n",a[i]);
 }
 printf("The no. of swapping required is:%d",count);
 getch();
}