C++ program for Sorting Dates using Selection Sort

3.5
// C++ program for sorting dates using selectio0n sort
#include<bits/stdc++.h>
using namespace std;
struct date
{
    int day;
    int month;
    int year;
};

int main()
{
    struct date input[5];
    for(int i=0; i<5; i++)
    {
        cin>>input[i].day;
        cin>>input[i].month;
        cin>>input[i].year;

    }
    for (int i=0; i<4; i++)
    {
        for (int j=i+1; j<5; j++)
        {
            if (input[i].year > input[j].year)
            {
                struct date temp = input[i];
                input[i] = input[j];
                input[j] = temp;
            }
            else if (input[i].year == input[j].year && input[i].month > input[j].month)
            {
                struct date temp = input[i];
                input[i] = input[j];
                input[j] = temp;
            }
            else if (input[i].year == input[j].year && input[i].month == input[j].month && input[i].day > input[j].day)
            {
                struct date temp = input[i];
                input[i] = input[j];
                input[j] = temp;
            }

        }
    }


    for(int i=0; i<5; i++)
    {
        cout<<input[i].day<<" "<<input[i].month<<" "<<input[i].year;
        cout<<endl;
    }
}

This program is contributed by Dinesh T.P.D. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.5 Average Difficulty : 3.5/5.0
Based on 2 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.