In: Computer Science
8) Sorting - C++
You have this function that sorts any vector of char data:
void good_bubble(vector<char> & data, vector<char>::size_type start, vector<char>::size_type end) { vector<char>::size_type loop{0}, cur; bool done{false}; while (loop <= end-start+1 && !done) { done = true; for (cur = start; cur <= end-1-loop; ++cur) { if (data[cur] > data[cur+1]) { swap(data[cur], data[cur+1]); done = false; } } ++loop; } return; }
But now you have to sort Date objects! As luck would have it, the Date class provides the method:
bool Date::greater(const Date & other) const;
Please show your changes to only the lines from above that would need to change to make your overload of good_bubble sort a vector of Date objects.
Updated code that would sort Date objects:
.
void good_bubble(vector<Date> &data, vector<Date>::size_type start, vector<Date>::size_type end)
{
vector<Date>::size_type loop{0}, cur;
bool done{false};
while (loop <= end - start + 1 && !done)
{
done = true;
for (cur = start; cur <= end - 1 - loop; ++cur)
{
// use greater instead of >
if (data[cur].greater(data[cur + 1]))
{
swap(data[cur], data[cur + 1]);
done = false;
}
}
++loop;
}
return;
}
.
Changed code has been highlighted.