Close

Linear search

Given an array of numbers, how do we search for a given number?
For example, given an array [6, 50, 24, 36, 42], the number 36 is present at the index 3 starting with 0. The number 17 is not present any where.
The simplest algorithm to solve this problem is to check each element of the array starting from the first element and search till the target element is found or we reach the end of the array. This most fundamental algorithm is called the linear search.
Here is the pseudo code for the same.

ind = 0
while ind < length(Array)
   if Array[ind] == target
      print ind
      break
   ind = ind + 1
print -1

Leave a Reply

Your email address will not be published. Required fields are marked *