list< int > myList = { 2, 6, 12, 13, 15, 18, 20};
cout << binary_search(myList.begin(), myList.end(), 20) ;
what will be its time complexity?
list< int > myList = { 2, 6, 12, 13, 15, 18, 20};
cout << binary_search(myList.begin(), myList.end(), 20) ;
what will be its time complexity?
@kt62495
hello karan,
here we are given list and list stl is very similar to the link list i.e we need to to traverse list from start to reach a particluar index.
if i represent recurrence relation for binary search on list then it will be something as ->
T(n)=T(n/2) + O(n) // O(n) because to reach to mid element we need to traverse half of the list which is equivalent to O(n) work.
On solving this recurrence relation we get O(n) time complexity