C program to sort elements of Array in Ascending order using DMA.....(Using pointers).


#include<stdlib.h>
#include<stdio.h>
int main()
{
int *p;
int n,i,j,flag=0,temp=0;
printf(" Enter size of Array:");
scanf("%d",&n);
p=(int*) calloc(n,sizeof(int));
if(p==NULL)
{
printf("**Memory is not allocated**");
exit(0);
}
else
{
printf("**Memory is successfully allocated**");
printf("\n Enter %d elements in Array:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<(n-i)-1;j++)
{
  if(*(p+j)>*(p+(j+1)))
  {
    temp=*(p+j);
    *(p+j)=*(p+(j+1));
    *(p+(j+1))=temp;
    flag=1;
  }
}
if(flag==0)
{
break;
}
}
printf("After sorting elements of Array are:");
for(i=0;i<n;i++)
{
printf("\n%d",*(p+i));
}
}
return 0;
}


**** INPUT && OUTPUT ****



Post a Comment

0 Comments