c program for modified bubble sort

Posted: February 4, 2012 in Lab 4
#include<stdio.h>
void swap(int *b,int *c)
{
int x;
x=*b;
*b=*c;
*c=x;
}
void main ()
{
int a[10],i,j,flag;
printf("enter the unsorted elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
	flag=0;
for(j=0;j<4-i;j++)
{
	if(a[j]>a[j+1])
	{
		flag=1;
		swap(&a[j],&a[j+1]);
	}
	}
if(flag==0)
	break;
}
printf("sorted elements are\n");
for(i=0;i<5;i++)
printf("%d\n",a[i]);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s