Saturday, 14 January 2012

Bubble sort

Here is the C implementation of Bubble sort.

 /*Bubble sort*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,size,temp,a[50];
clrscr();
/*Scan*/
printf("Enter the number of elements to be sorted:");
scanf("%d",&size);
printf("Enter the elements to be sorted:");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}  
/*Sort*/
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
/*Print*/
printf("The sorted elements are:\n");
for(i=0;i<size;i++)
{
printf("%d\t",a[i]);
}
getch();
}

No comments:

Post a Comment