Suppose you are given a list or an array of items. You are searching for a particular item. How do you do that?

Find the number 13 in the given list.

You just look at the list and there it is!

Now, how do you tell a computer to find it?

A computer cannot look at more than the value at a given instant of time. So it takes one item from the array and checks if it is the same as what you are looking for.

The first item did not match. So move onto the next one.

And so on…

This is done till a match is found or until all the items have been checked.

In this algorithm, you can stop when the item is found and then there is no need to look further.

So how long would it take to do the linear search operation? In the best case, you could get lucky and the item you are looking at maybe at the first position in the array!

But in the worst case, you would have to look at each and every item before you find the item at the last place or before you realize that the item is not in the array.

The complexity of linear search is therefore O(n).

If the element to be searched lived on the the first memory block then the complexity would be: O(1).

The code for a linear search function in JavaScript is shown below. This function returns the position of the item we are looking for in the array. If the item is not present in the array, the function will return null.

### Example in Javascript

``````function linearSearch(arr, item) {
// Go through all the elements of arr to look for item.
for (var i = 0; i < arr.length; i++) {
if (arr[i] === item) { // Found it!
return i;
}
}

return null;
}``````

### Example in Ruby

``````def linear_search(target, array)
counter = 0

while counter < array.length
if array[counter] == target
return counter
else
counter += 1
end
end
return nil
end``````

### Example in C++

``````int linear_search(int arr[],int n,int num)
{
for(int i=0;i<n;i++){
if(arr[i]==num)
return i;
}
return -1;
}``````

### Example in Python

``````def linear_search(array, num):
for i in range(len(array)):
if (array[i]==num):
return i
return -1``````

What if you are searching the multiple occurrences of an element? For example you want to see how many 5’s are in an array.

Target = 5

Array = [ 1, 2, 3, 4, 5, 6, 5, 7, 8, 9, 5]

This array has 3 occurrences of 5s and we want to return the indexes (where they are in the array) of all of them.

This is called global linear search and you will need to adjust your code to return an array of the index points at which it finds your target element.

When you find an index element that matches your target, the index point (counter) will be added in the results array. If it doesn’t match, the code will continue to move on to the next element in the array by adding 1 to the counter.

``````def global_linear_search(target, array)
counter = 0
results = []

while counter < array.length
if array[counter] == target
results << counter
counter += 1
else
counter += 1
end
end

if results.empty?
return nil
else
return results
end
end``````

## Why linear search is not efficient

There is no doubt that linear search is simple. But because it compares each element one by one, it is time consuming and therefore not very efficient. If we have to find a number from, say, 1,000,000 numbers and that number is at the last position, a linear search technique would become quite tedious.

So you should also learn about bubble sort, quick sort and other more efficient algorithms.