C Program to Sort a List of an Array Element

There are many Sorting algorithms, which sort a list of an array elements.

#include<stdio.h>
int main()
{
  int n, i, j;

  printf("Enter array range(no of array element): ");
  scanf("%d",&n);

  int  arr[n];

  printf("Enter the array element: ");

  // take input(Array elements)
  for(i=0;i<n;i++)
  {
    scanf("%d",&arr[i]);
  }

  // now, sort array elements
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {
      if(arr[i]>arr[j])
      {
        int temp=arr[i];

        arr[i]=arr[j];

        arr[j]=temp;
      }
    }
  }

  // display sorted array
  printf("Sorted array elements are:\n");
  for(i=0;i<n;i++)
  {
    printf("%d\n",arr[i]);
  }

  return 0;

}

Output:-

Enter array range(no of array element): 5
Enter the array element: 90
23
5
60
40
Sorted array elements are:
5
23
40
60
90

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 program using Arrays

Examples on multidimensional Array in C

Leave a Comment

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