C++ Program to sort a list (descending order)

//sort_desc
#include <iostream>
using namespace std;
int main()
{  
    int n;        //size of list
    int i,j;         //for loops for reading, swapping etc
    double temp;    //swapping
    cout<<"Enter the size of the list"<<endl;
    cin>>n;
    cout<<"Enter the elements of the list\n";
    double a[n];    //the list of no.s to be sorted
    for (i=0;i<n;i++)    //reading the elements
    {
    cin>>a[i];
    }
    for (i=0;i<n;i++)    //sorting
    {
        for (j=i;j<n;j++)
        {
            if (a[i]<a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    cout<<"\nThe sorted list is \n";
        for (i=0;i<n;i++)
    {
    cout<<a[i]<<endl;
    }
    return 0;
}
  

I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.



Leave a Reply

Your email address will not be published. Required fields are marked *