EmbLogic's Blog

C program for sorting an array in asscending order

#include<stdio.h>
int main()
{
int a[100],i,j,n,temp;
printf(“How many no. you want to add in an array:”);
scanf(“%d”,&n);
printf(“Enter element of an array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Array before sorting is:\n”);
for(i=0;i<n;i++)
{
printf(“%d\n”,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(“Array after sorting is:\n”);
for(i=0;i<n;i++)
{
printf(“a[%d]=%d\n”,i,a[i]);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>