C program to sort an array of ten elements in an ascending order


An array is a group of related data items that are stored with single name.

For example, int student[30];

Here, student is an array name which holds 30 collection of data items, with a single variable name.

Operations

The operations of an array are explained below −

  • Searching − It is used to find whether a particular element is present or not.

  • Sorting − Helps in arranging the elements in an array either in ascending or descending order.

  • Traversing − Processing every element in an array, sequentially.

  • Inserting − Helps in inserting the elements in an array.

  • Deleting − Helps in deleting an element in an array.

In this program, we are using the swapping technique to sort the elements in an ascending order.

The logic that we applied to sort an array in ascending order is as follows −

for(i=0;i<10-1;i++){
   for(j=i+1;j<10;j++){
      if(element[i]>element[j]){
         temp=element[i]; //swapping element[i] with element[j]
         element[i]=element[j];
         element[j]=temp;
      }
   }
}

Program

Given below is the C program to sort an array of ten elements in an ascending order

 Live Demo

#include<stdio.h>
int main(){
   int element[10],i,j,temp;
   printf("enter 10 integer numbers:");
   for(i=0;i<10;i++){
      scanf("%d",&element[i]);
   }
   for(i=0;i<10-1;i++){
      for(j=i+1;j<10;j++){
         if(element[i]>element[j]){
            temp=element[i]; //swapping element[i] with element[j]
            element[i]=element[j];
            element[j]=temp;
         }
      }
   }
   printf("Elements are now in ascending order:");
   for(i=0;i<10;i++)
      printf("%d
",element[i]);    return 0; }

Output

The output is given below −

enter 10 integer numbers:
12
23
34
1
22
34
56
45
33
78
Elements are now in ascending order:1
12
22
23
33
34
34
45
56
78

Updated on: 15-Mar-2021

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements