linear search algorithm in Data Structure | Learn Data Structures and Algorithms with Python

Akansha Yadav walks through linear search in Python: a function that loops through an array comparing each element to a target value and returns the index if found or -1 if not. Best for small or unsorted datasets when simplicity matters more than speed.
Chapters
And after so long, I'm going to post about data structure and algorithm. Data structure and algorithm is important if you are going to apply for the top MNC companies or top companies like Google, Microsoft, Flipkart, Amazon. All these companies are going to ask you about DSA, that how strong your aptitude is and how strong your DSA is. So let's get started with the first topic, that is the searching algorithm of linear search. Linear search means very, very important. In the linear search, we are going to discuss about Basic stuff of using it to search any element. If you want to search any element from a list, so generally what will happen exactly is you will be provided with a list or an array. Once you are provided with a list or an array, you have to search for one certain value. When you wanted to search for a certain value, you have to cross— you have to basically use some methods to do it. You can go with a simple, you can go according to what has been, uh, you know, instructed to you. So let's first see what exactly the linear search is. In case of the linear search, the linear search is basically used when you wanted to sequentially search for any item. You are looking for any value, so you have to go one by one and you have to check it. This is useful, this is ideal only for the small data set. If you are going for the unsorted data set, then only you can use linear search. Now, how to write down the program for that? So for implementation, what exactly you have to do is you have to set up, you have to write down a function where you are going to pass two arguments. First should be your list, another should be your target value. Once you have written the list and the target value, then you have to write down the loop. For a loop, you have to iterate your loop for each element, just like we do generally in the normal Python. You have to do the same. You have to say that, okay, each element using a loop, and then you have to compare the value. Suppose in a loop you have 5 values and the value that you are looking for is on the 4th index, then in that case you will go from the 1st to 4th. Once you are going from 1st to 4th, you will check each and everything, whether the first value is equal to the 4th value. If yes, then it will return the index value, otherwise no. If it is the second value, then it will return the index value, otherwise No, same way you have to continue comparing the value with the target value, and once the value is found, it will print it. Otherwise, I want you to print -1. So it means at the end of the loop, you have to return -1 in case none of the match case is found. This is useful only in the case of the small data set. This is useful in the case of unsorted list. This is useful when you are prioritizing, uh, the simplicity rather than going for the time complexity. Although in DSA time complexity is the important aspect, but if your instructor asks you that you just have to solve this, you need not to worry about the time complexity, then you have to go with the linear search because this is the easiest way. In this case, you need not to do any difficult thing. You just have to define a function, define a loop, return certain things, and that's it. Now in the function you have to pass two things. First should be your target value, second should be the list in which, in which you are going to find that target value. So let's implement it practically. Now this is what you need to do. You have to define a function. While defining a function, now my function is naming as like linear search. After that I am passing two arguments. First is my array and second is my target value. Then I have to traverse through the list. Traverse through the list means you are going to iterate through the list. Now I have written for index in range, then bracket length of an array. Because I am not mentioning any fixed value, maybe in case in future I am going to, you know, I can update the list with the more number of values. So I'm just going to make it, uh, I'm going to make it dynamic so that we can use it with multiple arrays. So now I have written for index in range bracket length of an array, and then I am checking whether the array index is equal to equal to the value which is the target value. If this is the case, then I want it to return the index value of that. You can see I'm using the f-string over here. Quotation begins, element target found at index, this is the index value. And then I'm here, I'm returning f-string element this not found. Apart from this, you can also return just directly saying, uh, instead of writing this, you just say -1. And for an example use case, I have created a rough array. After that, I have specified the target value. Then I am calling my linear search function under the result variable, and we are getting the print statement. Over here. Now I'm going to execute this program, and you can see that element 70 is found at, at index 3. Now if I'm going to update multiple values in this case, then it will go like this. Like, I have updated 4 to 5 values. Now I'm going to look for 96. Then I can update the value, I can update the list as well. And this is the output that we are getting, that element 96 is at index 96. 9. Element 96 found at index 7. Now if I'm going to make something which is not available, like 97 is not available in our list, now I'm going to run this. Now this is the output. You can see we are getting -1. In case you don't want -1, you wanted to make it more clear, then you can print that it is not found. And you can see over here we are able to get the output saying element 97 not found in the list. So this is how you will implement the linear search. In case of linear search, as I said, you have to follow certain steps, like you have to define a function with two arguments: list and target value. After this, you have to write a loop to iterate through the values of loop— of list, sorry. Return compare with each value. In case the match is found, return the index of the value, else return -1 or item not found. This is the step that you have to follow for the linear search. So we'll meet in the next video. Up till then, just try to practice linear search. I have added 2 questions in the description for you to practice. Don't forget to check them out. Bye-bye.










