# What is Linear Search Algorithm in Data Structure

\$\$/\$\$

In this segment, we will discuss this naive search technique, which is also the most intuitive one. If you are told to search for something in a list, you will most likely do so this way. So, let’s see what linear search is and how efficient it is.

\$\$/\$\$

So, in this video, you learned how linear search algorithm works. It basically compares the element you are searching for with every element in the array. It keeps on comparing till the algorithm finds the required element or it reaches the end of the array to conclude that the element does not exist in the array.

## Video Transcript

So now we'll talk about linear search. So suppose you are given a collection of objects and you are supposed to find whether an element exists in that collection or not. So what is the most basic way to do it? Well, the most basic way would be to compare each and every element from that collection and see if it equals to the element which I want to find. If I find it, then we are done. And if I iterate over all the objects in my collection and still don't find that element, then this would imply that the element does not exist. So let us take a simple example and see how this works. Suppose I'm given an array of numbers like this.

Let me call this array array A. Now suppose I want to find an element x and check if it exists inside this array or no. Let me take x equals to seven. So the idea behind linear search is a very basic one. What I'll do is that I will take this array and I will compare each and every object in that array with the element which I want to find. So I will start with number six and check if six equals to seven. Since they are not equal, I say no, I haven't found this element and I move on to my next element. My next element here is three. Again I'll compare it with seven and find that since they are unequal, again my search has been unsuccessful. So now I'll move on to the next element here, which is eight. On comparing it again with seven, I find that since they both are not equal, so I must move on to my next element, which is seven in this case. So in this step, when I compare this seven with the seven which I want to find, I see that they both are equal. So I say that I have finally found my element seven inside this array and it is located at index number 0123. So I'll say that I found the element number seven at index number three. So how many steps did it take for me to find it? Step one, step two, step three and step four. So in a total number of four steps I was able to successfully find number seven here. Now let us take another simple example with the same array, suppose I have to find whether element x exists inside this array or no. And in this case I'll take my x equal to one. So repeating the same procedure.

Repeating the same procedure. Now what I have to do is compare this one with each and every element in this array. So in step one, I compare it with six and I find that they are unequal. So I move on to the next element which is three. I again compare it with one and find that they are unequal. Now I move to my elements number three which is eight. On comparing it with one I find again unequal and similarly, on comparing it with seven, again I'll see that since they both are unequal my search has been unsuccessful. Now, lastly, I'll check the last element of my array which is nine and which is also not equal to one. But do I have to check anything beyond this? No. I reached the end of my array and on iterating through each and every element of this array I could not find the element which I wanted to seek which was one in this case. So what will I say? I'll say that my overall search over this array for finding element one was unsuccessful. And how many steps did it take for me to conclude that? Step 1 2 3 4 and 5. So it took me a total number of five steps to see that one does not exist inside this array. And also five was the total number of elements in this array. And I had to do five comparisons and check step by step whether it equals to the element which I wanted to find.

## Video Recap

• Linear search in java and other languages is used to find an element in a collection of objects

• The most basic way is to compare each element with the element to be found

• If found, the search is successful, else it implies that the element does not exist

• Linear search in java and other languages involves iterating through each element of the collection

• Example: searching for element 7 in an array of numbers

• Compare each element with 7 until it is found at index 3 after 4 steps

• Another example: searching for element 1 in the same array

• Compare each element with 1 until the end of the array, which takes 5 steps

• If the element is not found after iterating through all elements, the search is unsuccessful

• Total number of steps taken is equal to the number of elements in the array

• Linear search in java and other languages is a simple but inefficient search algorithm.

Brute-force searching :
Brute-force searching is also known as exhaustive searching and simply means to check all possible configurations for a given problem. It is easy to implement and will most definitely find the solution but it is quite time-consuming.

For example :

If you have a problem set in a countable space (chess moves are countable, passwords are countable, continuous stuff is uncountable) brute force will explore this space considering all solutions equally. In the chess example, you want to checkmate your opponent. This is done via a sequence of moves, which is countable. Brute force will go through all sequence of moves, however unlikely they may be. The word unlikely is important because it means that if you have knowledge about your problem (you know what is unlikely to be the solution, like sacrificing your queen), you can do much better than brute force.