Thursday, March 3, 2011

writw a program to sort the given array using bubble sort


//       implementation of Bubble sort
 #include<stdio.h>
 #include<conio.h>
   void bsort(int[],int);
   void main()
 {
  int ar[50],n,i;
   clrscr();
   printf("\n\n How many element do u want to enter...");
   scanf("%d",&n);
 printf("\n\n Enter Array element:");
    for(i=0;i<n;i++)
scanf("%d",&ar[i]);
   bsort(ar,n);
   printf("\n\n The sorted array is a shown below..\n");
    for(i=0;i<n;i++)
     printf("%d",ar[i]);
getch();
 }
  void bsort(int ar[],int n)
    {
     int j,k,temp,i,ctr=0;
      for(j=0;j<n-1;j++)
 {
  for(k=0;k<(n-1)-j;k++)
 {
  if(ar[k]>ar[k+1])
{
temp=ar[k];
ar[k]=ar[k+1];
ar[k+1]=temp;
 }
printf("\n\n Array after iteration%d is--",++ctr);
  for( i=0;i<n;i++)
printf("\t%d",ar[i]);
   }
     }
}




  





No comments:

Post a Comment