Linear Search
Jun 1, 2025
This algorithm works well on any kind of array.
- The element whose position needs to be found will be compared with each element in the array one after another.
- If a match is found, its index will be returned. Otherwise, -1 will be returned.
public int linearSearch(int[] arr, int a){
int n=arr.length;
for (int idx = 0; idx < n; idx++) {
if (arr[idx]==a) {
return idx;
}
}
return -1;
}Time Complexity: O(n)
Get Harshavardhana P’s stories in your inbox
Join Medium for free to get updates from this writer.
Space Complexity: O(1)






