C++ Show any changes needed to overload the following good bubble function to so
ID: 3572123 • Letter: C
Question
C++ Show any changes needed to overload the following good bubble function to sort a vector of Date objects:
void good_bubble(vector & data, vector::size_type start, vector::size_type end)
{
vector::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;
}
You happen to know that the Date class provides the method: bool Date::greater(const Date & other) const; (. . . if this helps). Please show only the [parts of] lines that must change!
Explanation / Answer
while (loop <= end-start+1 && !done)
Do This :- while (loop < end-start-1 )
for (cur = start; cur <= end-1-loop; cur++)
Do This :- for (cur = start; cur < end-1-loop; cur++)
while (loop <= end-start+1 )
Do This :- while (loop <= end-start-1 && !done)
So, your code will work after one of the above changes.
Note :- remove the boolean done from the code specially from while loop condition.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.