Saturday, September 10, 2011

C Program To Implement Bubble Sort

Bubble Sort 

The bubble sort may be notoriously slow, but it's concept is simple in the all sorting
algorithms, so that reason is a good beginning for our exploration of sorting
techniques. 

Algorithm


1. Compare each pair of adjacent elements from the beginning of an array
2. If they are in reversed order, swap them.
3. If at least one swap has been done, repeat steps 1&2

Program

#include<stdio.h> 
main() 

int a[100],i,n,j,tem=0; 
printf("enter the limit"); 
scanf("%d",&n); 
printf("enter the numbers"); 
for(i=0;i<n;i++) 
scanf("%d",&a[i]); 
for(i=0;i<n-1;i++) 
for(j=0;j<n-1-i;j++) 
if(a[j]>a[j+1]) 

tem=a[j]; 
a[j]=a[j+1]; 
a[j+1]=tem; 

printf("sorted array"); 
for(j=0;j<n;j++) 
printf("%d\n",a[j]); 
}

Output

enter the limit 5
enter numbers 2 1 3 5 4
sorted array is
1
2
3
4
5




Tags: C program to implement bubble sort,C program to perform bubble sort ,Bubble sort in c,Free bubble sorting program,bubble sort algorithm,bubble sort flowchart

No comments:

Post a Comment