Junior — Middle
What is the asymptotic complexity of binary search when used for searching in linked lists?
sobes.tech AI
Answer from AI
The asymptotic complexity of binary search in an array is O(log n), since access to an element by index occurs in O(1).
However, when using binary search in a linked list, the situation changes because accessing the middle element requires traversing half of the list, which takes O(n) time.
Thus, each step of binary search requires O(n) to access the element, and there are O(log n) such steps. The overall asymptotic complexity is O(n log n).
Therefore, binary search is inefficient for linked lists, and linear search with O(n) complexity is usually used for them.