in ques 7 please explain how manual time complexity is o(m+n)) and not o(max(m,n))?
Vector doubt quiz
sorry question no. 6
The answer to question 6 is O(1)
The time complexity of manually swapping the elements is O(m+n).
O(m+n) is clearly greater than O(max(m,n)).
We have to swap the elements, resize the greater vector and increase the size of the smaller vector to perform successful swap. So we take O(m+n) instead of O(max(m,n)).
Why is the answer O(1)?
swap function of the two vectors is an inbuilt function. And its complexity is O(1).
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
Given two vectors v1 and v2, we can swap the contents of the two vectors manually using a O(N + M) time algorithm where N = v1.size() and M = v2.size().
However the vector class provides an inbuilt function swap().
What is the time complexity of v1.swap(v2) ?
how come the swap function do it in o(1)?
does it use pointer? can we also use pointer or swapping vectors?
The swap function’s time complexity is O(1).
There is no need to go to the inner functionality of the swap function. Just remember that its time complexity is constant.
http://www.cplusplus.com/reference/vector/vector/swap/
Check out these links for any further information.