Vector Bubble Sort c++

I have class Date, functions bubbleSort, isAfter and printVector. So my task is: Use the function bubbleSort to sort vector type objects Date(using function isAfter which compares dates). I've done something but it doesn't works, so can anyone help me with this? Function bubble sort(doesn't works with "Date", works fine with integers ,strings...). Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//isAfter
template<>
bool isAfter(const Date &first, const Date &second) {
    if (first.getYear() == second.getYear()) {
        if (first.getMonth() == second.getMonth()) {
            if (first.getDay() == second.getDay()) {
                cout << first.toString() << " is equal to " << second.toString() << endl;
                return false;
            } else if (first.getDay() > second.getDay()) {
                cout << " " << first.toString() << " is after " << " " << second.toString() << endl;
                return true;
            } else if (first.getDay() < second.getDay()) {
                cout << " " << second.toString() << " is after " << " " << first.toString() << endl;
                return true;
            }
        } else if (first.getMonth() > second.getMonth()) {
            cout << " " << first.toString() << " is after " << " " << second.toString() << endl;
            return true;
        } else if (first.getMonth() < second.getMonth()) {
            cout << " " << second.toString() << " is after " << " " << first.toString() << endl;
            return true;
        }
    } else if (first.getYear() > second.getYear()) {
        cout << " " << first.toString() << " is after " << " " << second.toString() << endl;
        return true;
    } else if (first.getYear() < second.getYear()) {
        cout << " " << second.toString() << " is after " << " " << first.toString() << endl;
        return true;
    }
    return false;
}

//bubbleSort
template<typename T>
void bubbleSort(vector<T> &vec) {
    bool swapp= true;
    while (swapp) {
        swapp= false;
        for (unsigned int i = 0; i < vec.size()- 1; i++) {
            if (isAfter(vec[i + 1], vec[i])) {
                swap(vec[i], vec[i + 1]);
                swapp = true;
            }

        }
    }
}

I don't know what my mistake is? If someone can help me it will be much appreciated!
Given two dates a and b, is_after(a, b) should be true if and only if a is ordered-after b.

For consistency with the standard library, let's write is_before instead. Let's also rename it operator<, operator less-than.

One date is less-than another if it occurred earlier in time:
1
2
3
4
5
6
7
8
9
10
bool operator<(Date a, Date b)
{
  if (a.getYear() < b.getYear()) return true;
  if (b.getYear() < a.getYear()) return false;
  // years are equivalent; tiebreak using months
  if (a.getMonth() < b.getMonth()) return true;
  if (b.getMonth() < a.getMonth()) return false;
  // months are equivalent; tiebreak using days
  return a.getDay() < b.getDay();
}


As usual, you can call this function by name
operator<(first_date, second_date)
But you may also call it using the < sign:
first_date < second_date

Now inside bubblesort, just use '<' to compare dates. To compare in reverse order, note that b < a == a > b.

As is, your bubble-sort formulation will fail when passed a zero-size vector. Nothing's wrong with your algorithm as far as I can see - you're just being bitten by a design flaw in the standard library.

Problem is, vec.size() is an unsigned int. If its value is zero, subtracting one from it yields the largest possible value of unsigned int.
Last edited on
Topic archived. No new replies allowed.