Home / Programs / Sort array elements in c programming language
🚀 Programming Example

Sort array elements in c programming language

👁 1,079 Views
💻 Practical Program
📘 Step Learning
Sort array elements

💻 Program Code

#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;

}


                        

🖥 Program 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
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.