C++ program for Sorting Dates using Selection Sort


Date is number day, month and year. There are various ways to display a date.

Here, we have a program to sort dates using selection sort. So let's learn about things that are used in this concept.

Sorting dates

The concept of sorting dates needs a clear and well-versed knowledge of dates and their validations. Before we try sorting technique we need to check if the date inputted by the user is a valid date of not like 29-2 is only valid for leap years.

After validation of dates comes the sorting of dates. For sorting, we will go in reverse order sorting years then for same year sorting months and then for the same month sorting dates.

Selection Sort

Selection sort is a sorting algorithm that sorts elements by finding the minimum element and placing it at the beginning of the array and then shortening the array. Hence, placing each element at their respective place in ascending order.

This program uses the selection sorting technique for sorting dates that are inputted by the user.

Let’s take 3 dates in unordered form and then sort these dates.

Input :
4  1  2012
31 5  2019
19 12 2012

Output :
4  1  2012
19 12 2012
31 5  2019

Explanation

As all the dates are valid, the program will sort these dates and sort them in ascending order.

Example

 Live Demo

#include<iostream>
using namespace std;
struct date{
   int day;
   int month;
   int year;
   int valid = 1;
};
int datevalidate(int dd, int mm, int yy){
   if(yy>=100 && yy<=9999){
      if(mm>=1 && mm<=12){
         if((dd>=1 && dd<=31) && (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12));
            else if((dd>=1 && dd<=30) && (mm==4 || mm==6 || mm==9 || mm==11));
               else if((dd>=1 && dd<=28) && (mm==2));
                  else if(dd==29 && mm==2 && (yy%400==0 ||(yy%4==0 && yy%100!=0)));
            else
               return 0;
            } else{
            return 0;
         }
      } else {
      return 0;
   }
}
int main(){
   cout<<"Enter 5 dates to be sorted:\n";
   struct date input[5];
   for(int i=0; i<5; i++){
      cout<<"Enter Date "<<(i+1)<<" : ";
      cin>>input[i].day;
      cin>>input[i].month;
      cin>>input[i].year;
      input[i].valid = datevalidate(input[i].day, input[i].month, input[i].year);
      if(input[i].valid){
         cout<<"Date is invalid";
         exit(0);
      }
   }
   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;
         }
      }
   }
   cout<<"Sorted dates are : \n";
   for(int i=0; i<5; i++){
      cout<<input[i].day<<" "<<input[i].month<<" "<<input[i].year;
      cout<<endl;
   }
}

Output

Enter 5 dates to be sorted:
Enter date 1 : 5 12 2019
Enter date 2 : 1 2 2012
Enter date 3 : 11 6 2324
Enter date 4 : 29 2 2652
Enter date 5 : 16 5 2012
Sorted dates are :
1 2 2012
16 5 2012
5 12 2019
11 6 2324
29 2 2652

Updated on: 19-Sep-2019

721 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements