Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bubble Sorting an Array of Objects in C++

I need to sort an array composed of the Date objects by using bubble sort. Objects include private attributes, so I tried to use friend functions. Currently, the program is running but the dates that are printed are unsorted, I guess there are some problems with the use of pointers in the swap and BubbleSortDates functions. My code is below.

#define N 10
#include <iostream>
using namespace std;

class Date;
bool compareDates(Date *date1, Date *date2);

class Date
{
    // Data fields
    int Year;
    int Month;
    int Day;

  friend bool compareDates(Date *date1, Date *date2);

public:
    Date(); // Constructor
    Date(int YearIn, int MonthIn, int DayIn);
    bool SetDate(int YearIn, int MonthIn, int DayIn);
    void Print();
};

Date::Date()
{
    Year = 1970;
    Month = 1;
    Day = 1;
}

Date::Date(int YearIn, int MonthIn, int DayIn)
{
    bool IsValid = SetDate(YearIn, MonthIn, DayIn);
    if (!IsValid)
    {
        Year = 1970;
        Month = 1;
        Day = 1;
    }
}


bool Date::SetDate(int YearIn, int MonthIn, int DayIn)
{
    if (YearIn <= 0 || MonthIn <= 0 || DayIn <= 0)
    {
        return false;
    }

    else
    {
        Year = YearIn;
        Month = MonthIn;
        Day = DayIn;
        return true;
    }
}

void Date::Print()
{
    cout << "The day is: " << Day << "/" << Month << "/" << Year << endl;
}

bool compareDates(Date *date1, Date *date2)
{
  if (date1->Year > date2->Year)
    return true;
  else if (date1->Year < date2->Year)
    return false;
  else
  {
    if (date1->Month > date2->Month)
      return true;
    else if (date1->Month < date2->Month)
      return false;
    else
    {
      if (date1->Year > date2->Year)
        return true;
      else if (date1->Year < date2->Year)
        return false;
      else 
        return true;
    }
  }
}

void swap(Date *xp, Date *yp)
{
    Date temp = *xp;
    *xp = *yp;
    *yp = temp;
}

void BubbleSortDates(Date datesIn[])
{
    for (int i = 0; i < N - 1; i++)
    {
      for (int j = 0; j < N - i - 1; j++)
      {
        Date *date1 = &datesIn[i];
        Date *date2 = &datesIn[j];
        if(compareDates(date1, date2))
        {
          swap(date1, date2);
        }
      }
    }
    for (int i = 0; i < N; i++)
    {
      datesIn[i].Print();
    }
}

How can I correct those functions? Thanks in advance.


1 Answers

There is a typo in the function compareDates in this else statement

  else
  {
    if (date1->Month > date2->Month)
      return true;
    else if (date1->Month < date2->Month)
      return false;
    else
    {
      if (date1->Year > date2->Year)
        return true;
      else if (date1->Year < date2->Year)
        return false;
      else 
        return true;
    }
  }

It seems you mean

  else
  {
    if (date1->Month > date2->Month)
      return true;
    else if (date1->Month < date2->Month)
      return false;
    else
    {
      if (date1->Day > date2->Day)
        return true;
      else if (date1->Day < date2->Day)
        return false;
      else 
        return true;
    }
  }

Within the function BubbleSortDates last elements of the array with indices N - i - 1 (in the inner for loop) stay untouched in each iteration of the loop.

for (int i = 0; i < N - 1; i++)
{
  for (int j = 0; j < N - i - 1; j++)
  {
  // ...

You need to write the loops like

for (int i = 0; i < N - 1; i++)
{
  for (int j = i + 1; j < N; j++)
  {

  //...
like image 179
Vlad from Moscow Avatar answered Feb 16 '26 17:02

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!