program to sort given array in ascending
order.
#include<stdio.h>
#include<conio.h>
int main()
{
int
a[20],i,j,n,temp;
clrscr();
printf("\n
Enter no. of elements for 1-D array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("
Enter element[%d] : ",i+1);
scanf("%d",&a[i]);
}
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;
}
}
}
printf("\n\n
Ascending order of inserted array is :
");
for(i=0;i<n;i++)
{
printf("\n
%d ",a[i]);
}
getch();
return
0;
}
Enter no. of elements
for 1-D array : 5
Enter element
[1] : 2
Enter element
[2] : 10
Enter element
[3] : 4
Enter element
[4] : 13
Enter element
[5] : 7
Ascending order of
inserted array is:
2
4
7
10
13
No comments:
Post a Comment