Please help me explain this. Why i am wrong?
Algorithm STL Quiz Question 2
Hey @meet1708
list< int > myList = { 2, 6, 12, 13, 15, 18, 20};
cout << binary_search(myList.begin(), myList.end(), 20) ;
- Output is 20 and time complexity is Linear in size of the list.
- Output is 1 and time complexity is Linear in size of the list.
- Output is 20 and time complexity is Logarithmic in size of the list.
- Output is 1 and time complexity is Logarithmic in size of the list.
Then answer will be option 2 as myList is not an array, it’s a linked list. If you are not familiar with linked list then there will be a section of linked list in your course. When you will do that you will be able to find out why is it option 2.
So in linked list, if we use binary_search() STL it will take O(1) time. Right??
No it will take O(n) time
And binary search function return a boolean
ok thanks understood
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.