VOOZH about

URL: https://www.geeksforgeeks.org/dsa/two-way-linear-search-algorithm/

⇱ Two Way Linear Search Algorithm - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Two Way Linear Search Algorithm

Last Updated : 23 Jul, 2025

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.

What is Two-Way Linear Search?

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).

Algorithm for Two-Way Linear Search:

Below is the step-by-step algorithm for Two-Way Linear Search:

  • Initialize the search indices start=0 and end=N-1 (where N is the size of the array).
  • While the elements at indexes start and end are not equal to the search element, increment start by 1 and decrement end by 1.
  • Continue looping until start <= end; the loop terminates once it reaches the middle of the array.
  • If the element is found, return its index; otherwise, return -1.

Implementation of Two-Way Linear Search Algorithm:

Below is the implementation of Two-Way Linear Search Algorithm:


Output
Element is present at index 2

Time Complexity : O(N), where N is the size of input array arr[]
Auxiliary Space : O(1)

Comment
Article Tags: