The provided array must be sorted in this program in ascending order such that the elements are displayed from least to largest. Two loops can be used to accomplish this. The inner loop enables us to compare the element that was chosen with the other elements once the outer loop has chosen an element.

**Original array**

`5 2 8 7 1`

**Array after sorting**

`1 2 5 7 8`

The order of the elements will be such that the smallest element, in this example 1, which is the smallest, will appear on the far left. On the far right, the greatest piece, in this case, 8 will be visible.

## Logic Used to Sort an Array

The items may be sorted using a variety of reasoning. For the sake of simplicity, we will use a standard method used in ordinary living. In order to sort an array, we choose a certain element and position it correctly by comparing it to other components.

### Explanation

Take input array βaβ and no of elements(n) as 4

**Let us take elements for array a={7,8,12,3}.**

`1st iteration for(i=0;i`a[j]) i.e. if(a[0]>a[1]) i.e. if(7>8) false
2nd iteration for(j=i+1;ja[j]) i.e. if(a[0]>a[1]) i.e. if(7>12) false
3rd iteration for(j=i+1;ja[j]) i.e. if(a[0]>a[1]) i.e. if(7>3) true
temp=a[i]; i.e. temp=a[0] i.e. temp=7
a[i]=a[j]; i.e. a[0]=a[3] i.e. a[0]=3
a[j]=temp; i.e. a[3]=7

**So now are array is a={3,8,12,7}**

`2nd iteration for(i=1;i`a[j]) i.e. if(a[1]>a[2]) i.e. if(8>12) false
2nd iteration for(j=i+1;ja[j]) i.e. if(a[1]>a[3]) i.e. if(8>7) true
temp=a[i]; i.e. temp=a[1] i.e. temp=8
a[i]=a[j]; i.e. a[1]=a[3] i.e. a[1]=7
a[j]=temp; i.e. a[3]=8

**So now are array is a={3,7,12,8}**

`3rd iteration for(i=2;i`a[j]) i.e. if(a[2]>a[3]) i.e. if(12>8) true
temp=a[i]; i.e. temp=a[2] i.e. temp=12
a[i]=a[j]; i.e. a[2]=a[3] i.e. a[2]=8
a[j]=temp; i.e. a[3]=12

**So now are array is a={3,7,8,12}**

**Hence we have sorted the elements in ascending order a={3,7,8,12}.**

## Algorithm for Sort an Array Elements

- START
- INITIALIZE arr[] ={5, 2, 8, 7, 1 }.
- SET temp =0
- length= sizeof(arr)/sizeof(arr[0])
- PRINT "Elements of Original Array"
- SET i=0. REPEAT STEP 7 and STEP 8 UNTIL i<length
- PRINT arr[i]
- i=i+1.
- SET i=0. REPEAT STEP 10 to STEP UNTIL i<n
- SET j=i+1. REPEAT STEP 11 UNTIL j<length
- if(arr[i]>arr[j]) then

temp = arr[i]

arr[i]=arr[j]

arr[j]=temp - j=j+1.
- i=i+1.
- PRINT new line
- PRINT "Elements of the array sorted in ascending order"
- SET i=0. REPEAT the below steps UNTIL i < length
- PRINT arr[i]
- i=i+1.
- RETURN 0.
- END.

## C Program for Array Sorting

#include <stdio.h> int main() { int arr[] = {50, 20, 80, 70, 10}; int temp = 0; int length = sizeof(arr)/sizeof(arr[0]); printf("Elements of original array: \n"); for (int i = 0; i < length; i++) { printf("%d ", arr[i]); } for (int i = 0; i < length; i++) { for (int j = i+1; j < length; j++) { if(arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } printf("\n"); printf("Elements of array sorted in ascending order: \n"); for (int i = 0; i < length; i++) { printf("%d ", arr[i]); } return 0; }

**Time Complexity:** O(N2)

**Auxiliary Space:** O(1)

## Tips for Ascending Order Program

A sorting algorithm is used to reorder an array or list of elements in accordance with an element comparison operator. The new order of the items in the relevant data structure is determined using the comparison operator. Therefore, This article is the basic of the sorting algorithm and if you got these basic sorting methods you will be able to solve more difficult problems of sorting algorithms as in data structures and algorithms sorting bits of help in every advanced topic.

**Conclusion**

In the above article, we have discussed the general approach to sorting the array in ascending order. We hope this article helps you to enhance your knowledge and gives you a grasp to solve more similar problems. Also, you can visit PrepBytes for practicing more similar questions

**Other C Programs**

C Program for Binary Search

C Program to Add Two Numbers

C Program to Calculate Percentage of 5 Subjects

C Program to Convert Binary Number to Decimal Number

C Program to Convert Celsius to Fahrenheit

C Program to Convert Infix to Postfix

C Program to Find Area of Circle

C Program to Find Roots of Quadratic Equation

C program to Reverse a Linked List

C program to reverse a number

Menu Driven Program For All Operations On Doubly Linked List in C

C Program for Armstrong Number

C Program For Merge Sort For Linked Lists

C program for performing Bubble sort on Linked List

Hello World Program in C

Perfect Number Program in C

Leap Year Program in C

Odd Even Program in C

Selection Sort Program in C