Sort Elements using Dynamic Memory Allocation.

-- Leave a Comment
Hello Here is a C program to Arrange Elements in an array Using Dynamic Memory allocation. Pointer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
#include<stdlib.h>
main()
{
    int *p,n,j,i,tmp;
    printf("Enter the total no of Element to Arrange in:- ");
    scanf("%d",&n);
    p=(int *) malloc(sizeof(int)*n);
    printf("Enter the Emements:- \n");
    for (i=0;i<n;i++)
    {
        scanf("%d",(p+i));
    }
    for (i=0;i<(n-1);i++)
    {
        for(j=0;j<(n-i-1);j++)
        {
            if(*(p+j)>*(p+j+1))
            {
                tmp=*(p+j);
                *(p+j)=*(p+j+1);
                *(p+j+1)=tmp;
            }
        }
    }
    printf("Finally after arranged in Accesending Order:- \n");
    for (i=0;i<n;i++)
    {
        printf("%d \n",*(p+i));
    }
}

Have a good day!

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University. I know something about Linux, Android, Java, Nodejs, Unity3D and 3 dots :)

0 comments:

Post a Comment