Write a c program to sort n numbers in ascending order, Write a c program for bubble sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("How many elements? ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Sorted elements are\n");
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
Output:
How many elements? 5
Enter the elements
3
5
1
2
4
Sorted elements are
1
2
3
4
No comments: