Subscribe

RSS Feed (xml)

Thursday, October 29, 2009

Write a C program to Sort the list of integers using Bubble Sort

#include(stdio.h)  //note place '<' & '>' in place of '(' & ')'
#include(conio.h)
void bubble(int [ ], int); // func. declaration
void main( )
{
 int a[10],n,i;
clrscr();
printf("Enter the no. of elements u want to fill:");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0; i< n; i++)
scanf("%d",&a[i]);

bubble(a,n);  // calling function
getch();
}

void bubble(int b[], int no)  // called function
{
int i,j,temp;
for(i=0; i< no-1; i++)
{
for(j=0; j< no-i; j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
printf("\nAfter sorting : ");
for(i=0; i< no; i++)
printf("%5d",b[i] );
}

Output:- Enter the no. of elements u want to fill: 5
Enter the array elements : 8  1  90  5  4
After sorting : 1  4  5  8  90

1 comment: