Solved programming problem

Sort 1D Array By Ascending Order with Pointer in C.

Source code

#include<stdio.h>

int main()

{

    int i,j,n,swap;

    int *ptr[100];

    printf("Please Enter The array size: ");

    scanf("%d",&n);

    printf("Please Enter Array Element: ");

    for(i=0; i<n; i++)

    {

        scanf("%d",(ptr+i));

    }

    ///sorting part

    for(i=0; i<n-1; i++)

    {

        for(j=0; j<n-i-1; j++)

        {

            if(*(ptr+j)>*(ptr+j+1))

            {

                swap=*(ptr+j);

                *(ptr+j)=*(ptr+j+1) ;

                *(ptr+j+1)=swap;

            }

        }

    }

    printf("Your Sorted array: ");

    for(i=0; i<n; i++)

    {

        printf("%d ",*(ptr+i));

    }

    printf("
");

    return 0;

}

Output

Please Enter The array size: 3
Please Enter Array Element: 10 20 23
Your Sorted array: 10 20 23

Join the discussion

Add your comment