Slip no : 13_2 write c
program to sort elements of an array in asecnding order using dynamic memory
alloaction



#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
main()
{
int
n,i,temp,j; int *a; clrscr();
printf("\nEnter
how many elements : "); scanf("%d",&n);
a=(int
*)malloc(n*sizeof(int)); for(i=0;i<n;i++)
{
printf("\n
Enter element : "); scanf("%d",&a[i]);
}
printf("\n
Before sorting : "); for(i=0;i<n;i++)
{
printf("\t %d",a[i]);
}
for(i=n-1;i>=0;i--)
{
for(j=0;j<i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n
after sorting : "); for(i=0;i<n;i++)
{
printf("\t %d",a[i]);
}
getch();
}
No comments:
Post a Comment