C Program to Sort List of Array Elements Using Function & Pointer

Program description:- C Program to Sort the List of Array Elements Using Function and Pointer.

#include<stdio.h>
void sort(int *x);
int main()
{
  int a[5], i;
  int *pa;
  pa =  &a[0];

  printf("Enter array elements: ");
  for(i=0;i<5;i++)
  {
    scanf("%d",pa+i);
  }

  sort( &a[0] );

  printf("Sorted array is: ");
  for(i=0;i<5;i++)
  {
    printf("%d\t", *(pa+i));
  }

  return 0;
}

void sort(int *x)
{
  int i, j, temp;

  for(i=0;i<5;i++)
  {
    for(j=i+1;j<5;j++)
    {
      if( *(x+i) > *(x+j) )
      {
        temp = *(x+i);
        *(x+i) = *(x+j);
        *(x+j) = temp;
      }
    }
  }
}

Output:-

Enter array elements: 15
3
60
12
32
Sorted array is: 3 12 15 32 60

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Similar C programs on Pointers

Leave a Comment

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