Pages

Ads 468x60px

Saturday, 2 June 2012

C Program For BUBBLE SORTING


C Program For BUBBLE SORTING



Searching and sorting have been popular operations in elementry data structures. Although now a days many advanced sorting techniques are used to sort a set of data, the basic sorting methods are still popular and are the used frequently. Here C code for bubble sort is given. The algorithm for bubble sort is quite simple. In each iteration the adjacent elements are compared and they are swapped if their order is not correct.
 The complexity of this algorithm comes out to be O(n²).

CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,t,a[20];
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i]>=a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=1;i<=n;i++)
{
printf("%d\n",a[i]);
}
getch();
}

0 comments:

Post a Comment

Total Pageviews