Searching
Linear Search

int? LinearSearch(int[] array, int target){
int n = array.Length - 1;
for(int i = 0; i <= n; i++){
if(array[i] == target)
return i;
}
return -1;
}def linear_search(array, target):
for i in range(0, len(array)):
if (array[i] == target):
return i
return -1Time Complexity: O(n)
Binary Search
Binary search works only on a sorted set of elements.

Using Recursion:
Using Recursion
Time Complexity: O(log n)
Fact
If all the names in the world are written down together in order and you want to search for the position of a specific name, binary search will accomplish this in a maximum of 35 iterations.
Linear Search < Binary Search
Last updated
Was this helpful?