Search code examples
algorithmsearchtime-complexitylinear-search

Can linear search be executed in a O(log n) time by incrementing the loop variable by 2 instead of 1?


The crucial change in the code could be something like:

// while loop from 0 to n - 2; i initially = 0
if( arr[i + 1] != element && arr[i] != element) i += 2; 
else if(arr[i] == element){ cout << "Element present at: " << i; }  
else{ cout << "Element is present at: " << i + 1; return 0; } 

What do you think?


Solution

  • The answer is no. In the worst case you have to look at every element and there are n of them.