Sort an array Elements in Ascending Order in C
Sort an array Elements in Ascending Order in C. Array is the collection of similar data type and it store data on the basis of index value. To sort array elements you need to compare first element with next element and second element with third elements and so on.....
#include<stdio. h>
#include<conio. h>
#include<dos. h>
void main()
{
int i,a[10],temp,j;
clrscr();
printf("Enter any 10 num in array = \n");
for(i=0;i<=10;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nData before sorting = ");
for(j=0;j<10;j++)
{
delay(200);
printf(" %d",a[j]);
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\n\nData after sorting = ");
for(j=0;j<10;j++)
{
delay(200);
printf(" %d", a[j]);
}
getch();
}
Output
No comments:
Post a Comment