C++ Program to sort an array in descending order


In this program, You will learn how to sort an array in descending order in C++.


Array list is: 2 3 1 7 6

After sort: 7 6 3 2 1


Example: How to sort an array in descending order in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int a[10];
    int n, temp;

    cout << "Enter Size of an Array :";
    cin>>n;

    cout << "\nEnter  Array  Elements :";
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    for (int i = 0; i < n; i++) {
        for (int j = 1 + i; j < n; j++) {
            if (a[i] < a[j]) {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
    cout << "\nArray List in Descending Order :";
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }

    return 0;
}

Output:

Enter Size of an Array :5
Enter  Array  Elements :2 3 1 7 6
Array List in Descending Order :7 6 3 2 1