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;
}
  

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in computational material science. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT and Machine Learning 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.
[wpedon id="7041" align="center"]

Leave a Reply

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