(C++) CPP Programs - 14
Friday, December 14, 2007
14. Write a C program to sort n numbers in ascending order using Bubble sort and print both the given array and the sorted array with suitable headings.
/* Bubble sort*/
#include
#include
void main()
{
int n,i,j,a[30],temp;
clrscr();
printf("Enter how many digits?\n\a");
scanf("%d",&n);
printf("Enter the numbers one by one\n\a");
for(i=0;i <>
scanf("%d",&a[i]);
printf("\nThe array before sorting\n\a");
for(i=0;i <>
printf("%4d",a[i]);
/*bub now*/
for(i=0;i <>
{
for(j=0;j <>
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\nThe array after sorting\n\a");
for(i=0;i <>
printf("%4d",a[i]);
getch();
}
0 comments:
Post a Comment