2/23/2009

C Program To Perform Bubble Sort

First Program:
/* C Program For Sorting Given Set Of Numbers Using Bubble Sort Technique */
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10],i,n,temp,j;

 printf("Enter the size of an array:\n");
  scanf("%d",&n);
 printf("Enter the elements to an array:\n");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);

 for(i=0;i<n;i++)
  {
  for(j=0;j<n-1;j++)
   {
   if(a[j]>a[j+1])
    {
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
   }
  }
 printf("Sorted array elements are:\n");
 for(i=0;i<n;i++)
  printf("a[%d]=%d \n",i,a[i]);

}
Out Put:

Enter the size of an array:
5
Enter the elements to an array:
20
60
50
10
40
Sorted array elements are:
a[0]=10
a[1]=20
a[2]=40
a[3]=50
a[4]=60



Second Program:

/* C Program To Perform Bubble Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10],i,n,temp,j;
 clrscr();

 printf("Enter the size of an array\n");
  scanf("%d",&n);
 printf("Enter the element to an array\n");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);

 for(i=0;i<n;i++)
  {
  for(j=0;j<n-1;j++)
   {
   if(a[j]>a[j+1])
    {
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
   }
  }
 printf("Sorted element are\n");
 for(i=0;i<n;i++)
  printf("%d ",a[i]);
getch();
}
Out Put:
C Program To Perform Bubble Sort

No comments:

Post a Comment