# Write C++ program to sort array in ascending order Contents of article

# Write C++ program to sort array in ascending order

hello, friends in today’s article I am going to share C++ program to sort array in ascending order so let’s begin today’s article

so first, we have to understand what is sorting?

sorting means the process of arranging items in a specific order.

next, What do you mean by ascending order?

ascending order means an arrangement of items from smallest to largest.

## Explanation

There is various logic to sort given set of numbers. Here I am using a general algorithm which we apply in real life for simplicity.

In this program, we need to sort the given array in ascending order such that elements will be arranged from smallest to largest. This can be achieved through two loops. The outer loop will select an element, and the inner loop allows us to compare the selected element with the rest of the elements.

Example:

Original array: 3  2  1  6  4

Array after sorting: 1  2  3  4  6

now,

## Logic to sort array in ascending order

Step 1: Take the input size of the array and elements. stored it in the variable say size, n.

Example code:

###  Step 2: use one for loop as an outer for loop to select each element from an array, starting from 0 up to the size of the array.

Example code: Step 3: use another inner for loop to compare the selected element from the outer loop with the rest of the elements of the array.

Example code: Step 4: If any element is less than the selected element then swap the values as follows:

Example code: so finally the program is completed, check the below code:

## Output: 