Sort array elements in c programming language

C Programming Language Array in C Language (Article) Array in C Language (Program)

988

Program:

#include"stdio.h"
#include"stdlib.h"

int main()
{

int a[30],n,i,j,temp, sorted=0;

printf("\n How many numbers");
scanf("%d",&n);
 if(n>30)
  {
    printf("\n Too many Numbers");
    exit(0);
   }

printf("\n Enter the array elements \n");
 for(i=0 ; i < n; i++)
   scanf("%d", &a[i]);
 for(i = 0; i < n-1 && sorted==0; i++)
  {
    sorted=1;
    for(j = 0; j < (n - i) -1; j++)
      if(a[j] > a[j+1])
        {
          temp = a[j];
          a[j] = a[j+1];
          a[j+1] = temp;
          sorted=0;
         }
    }

printf("\n The numbers in sorted order \n");
 for(i=0 ; i < n; ++i)
  printf("\n %d", a[i]);

return 0;

}

Output:

 How many numbers5

 Enter the array elements
6
5
3
8
4

 The numbers in sorted order

 3
 4
 5
 6
 8 

Explanation:

None

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.