Wednesday 18 March 2015

Program to sort the given numbers using bubble sort Using C


# include <stdio.h>
# include <conio.h>
void main()
{
int a[10], t, i, j, n ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the numbers :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
for(i = 0 ; i < n - 1 ; i++)
{
for(j = 0 ; j < n - 1 ; j++)
{
if(a[j] > a[j + 1])
{
t = a[j] ;
a[j] = a[j + 1] ;
a[j + 1] = t ;
}
}
}
printf("\nThe sorted elemets are :\n\n") ;
for(i = 0 ; i < n ; i++)
printf("%d\t", a[i]) ;
getch() ;
}


RUN 1 :
~~~~~~~
Enter the limit : 5
Enter the numbers :
20 40 30 50 10
The sorted elemets are :
10 20 30 40 50

No comments:

Post a Comment