# What is the time complexity of a linear search performed using 2 pointers?

For an array, I'm using a left pointer (pointing to 0) and a right pointer (pointing to end).

For every iteration, if my search element is not found, I increment left and decrement right. This iteration continues until left <= right. Find the code snippet below

left = 0
right = length - 1

while left <= right:
// search logic
left += 1
right -= 1



What will be worst the time complexity of this algorithm?

Also, is this a type of linear search?

• what is the cost of "search logic"? Apr 27 at 21:12

$$O(n)$$, since you still will go through the entire array.
And yes, this is a linear search since $$O(n)$$ is linear.
• of course, constants do not make a difference in $O$ notation Apr 27 at 19:01