![]() |
VOOZH | about |
Two-Way Linear Search Algorithm is based on the principle of Linear Search, but the search is conducted from both ends towards the center of the array. In this article, we will learn about the basics of Two Way Linear Search Algorithm, its advantages, implementation etc.
Two Way Linear Search is a searching technique where we start our search from both the ends of the array simultaneously and move towards the center until we find the target element.
We maintain two pointers, one pointing at the start and other pointing at the end and compare elements at start and end with the target element. If any of them is equal to target, then we know that we have found the target element. Otherwise, we move start to the next index and move end to the previous index and again compare the corresponding elements. Two Way Linear Search stops either when we find the target element or when start and end cross each other (start > end).
Below is the step-by-step algorithm for Two-Way Linear Search:
Below is the implementation of Two-Way Linear Search Algorithm:
Element is present at index 2
Time Complexity : O(N), where N is the size of input array arr[]
Auxiliary Space : O(1)