C++ Program to Sort an Array in Ascending Order

In this post, we will learn how to sort an array in ascending order using C++ Programming language.

This program prompts the user to enter the size of the array and elements of the array. Then, it arranges the elements of the array in the ascending order using For Loop statement.

So, without further ado, let’s begin this tutorial.

C++ Program to Sort an Array in Ascending Order

// C++ Program to Sort an Array in Ascending Order
#include <iostream>
using namespace std;

int main(){
    int i, j, size, temp;
    int arr[25];
    
    // Asking for input
    cout << "Enter the total no. of elements: ";
    cin >> size;
    
    // Enter the elements
    cout << "Enter the elements of the array: " << endl;
    for (i = 0; i < size; i++){
        cin >> arr[i];
    }
    
    // Sorting elements in ascending order
    for (i = 0; i < size; i++){
        for (j = i; j < size; j++){
            if (arr[i] > arr[j+1]){
                temp = arr[i];
                arr[i] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    
    // Displaying output
    cout << "Elements sorted in the ascending order are: " << endl;
    for (i = 1; i <= size; i++){
     cout << arr[i] << endl;   
    }
    return 0;
}

Output

Enter the total no. of elements: 5
Enter the elements of the array: 
68 94 35 46 25
Elements sorted in the ascending order are: 
25
35
46
68
94

How Does This Program Work ?

    int i, j, size, temp;
    int arr[25];

In this program, we have declared four integer data type variables named i, j, size, temp and one array named arr.

    // Asking for input
    cout << "Enter the total no. of elements: ";
    cin >> size;
    
    // Enter the elements
    cout << "Enter the elements of the array: " << endl;
    for (i = 0; i < size; i++){
        cin >> arr[i];
    }

Now, this program prompts the user to enter the size and elements of the array.

    // Sorting elements in ascending order
    for (i = 0; i < size; i++){
        for (j = i; j < size; j++){
            if (arr[i] > arr[j+1]){
                temp = arr[i];
                arr[i] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }

Now, we used a for loop to sort the elements of the array in ascending order. For this, we compare each element with the element lying next to it.

If the value of the first element is greater than the second element, then their positions get interchanged.

    // Displaying output
    cout << "Elements sorted in the ascending order are: " << endl;
    for (i = 1; i <= size; i++){
     cout << arr[i] << endl;   
    }

And the elements sorted in the ascending order are displayed on the screen using cout statement.

Conclusion

I hope after going through this post, you understand how to sort an array in ascending order using C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published.