Sitemap
Harshavardhana P

Harshavardhana P

Learn, Grow, Share, Guide and Help Development | Technology | Self Improvement | Fitness | Mindset

Linear Search

Jun 1, 2025

This algorithm works well on any kind of array.

  1. The element whose position needs to be found will be compared with each element in the array one after another.
  2. 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)

--

--

Harshavardhana P
Harshavardhana P

Written by Harshavardhana P

0 followers

Learn, Grow, Share, Guide and Help Development | Technology | Self Improvement | Fitness | Mindset