8) Sorting - C++ You have this function that sorts any vector of char data: ...

Free

70.2K

Verified Solution

Question

Programming

8) Sorting - C++

You have this function that sorts any vector of char data:

  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;  }

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 wouldneed to change to make your overload of good_bubble sort a vectorof Date objects.

Answer & Explanation Solved by verified expert
4.2 Ratings (854 Votes)

Updated code that would sort 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)

        {

            // 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.


Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students