SORT THE ARRAY ELEMENTS IN ASCENDING ORDER - C-Tutorial

Latest

Saturday, 1 October 2016

SORT THE ARRAY ELEMENTS IN ASCENDING ORDER

SORT THE ARRAY ELEMENTS IN ASCENDING ORDER 

#include<stdio.h>
#include<conio.h>

main()
{
int i,j,n,a[10],temp=0;
clrscr();
printf("\n Enter the n value: ");
scanf("%d",&n);
printf("\n Enter the %d array elements ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n before sorting the values are: \n");
for(i=0;i<n;i++)
{
printf("\t %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 After sorting the elements are: \n");
for(i=0;i<n;i++)
{
printf("\t %d",a[i]);
}
getch();
}

No comments:

Post a Comment