Question: The very first container class we implemented was the random bag, which supported two operations: add, which adds an element to the random bag, and
The very first container class we implemented was the random bag, which supported two operations:
add, which adds an element to the random bag, and
removeRandom, which chooses a random element from the bag and returns it
Below is our implemented code for RandomBag class. First, RandomBag.hppNext, RandomBag, cpp:Your task in this problem is to answer the following questions. Before that, recall the following important fact:Fact: The vector: : erase function, takes time proportional to the distance between the element being removed and the last element of the Vector. The reason for this is that whenever a gap opens up in the vector, it has to shift all the elements after the gap down one spot, As a result, removing from the very end of a vector is quite fast it runs in time O but removing from the very front of a vector takes time OnQ points Look at the implementation of our RandomBag: :removeRandom function. What is its worstcase time complexity? How about its bestcase time complexity? How about its averagecase time complexity?Q points Based on your answer to the previous part of this question, what is the worstcase time complexity of removing all the elements of an nelement RandomBag? What's the bestcase time complexity? How about its average case?Q points In the preceding discussion, we mentioned that removing the very last element of a vector is much more efficient that removing an element closer to the middle. Write another member function RandomBag: :removeRandomFast so that it always runs in worstcase O time. In fact, there should only be two lines of code that are different from our current implementation.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
