Algorithms: Linear Search

Elnur
Star Gazers
Published in
4 min readFeb 22, 2021

--

If you are beginner in programming, you would probably want to practice your skills. When you do, you would see some coding challenges which is not easy to understand.

In this article, I will go through some of those algorithms and explain them.
So, let’s learn our first algorithms:

Searching for a Specific Element in the Array (Linear Search)

Note: You can also use lists instead of an array, the process is exactly the same

You will use this algorithm almost every time. It basically, search for the specific element and check if it exists in the array or not.

To understand how this algorithm work let’s have a look at the image below:

Let’s say we have an array with the size of six. This array has six different numbers in it.

Now, let’s say we want to write a program that says if a particular number exists in this array or not. To do that, we traverse the array and in each step, we check if our current array element is equal to the target element.

To understand it better, let’s dive into code:

Note: In this tutorial, I am using C# programming language, but the logic is the same in all languages

Getting Target Number From the User

Firstly, I have written some codes to get the number from the user:

  • Console.ReadLine() method takes input from the user.
  • int.Parse() method convert that input to the integer.

Note: In C#, we get the input as a string

  • I have declared an array with the 6 values

Then, I have written a method that returns a string value. This value will be either Found or Not Found.

Creating a Method Signature

So let’s create our method:

This method takes 2 parameters: the first parameter is the array, the second parameter is the target value we are searching for. The method return value will be a string type.

Writing LinearSearch Method

To search for a target value, we need to loop through the array. We will do that by using a for loop:

This loop will start from the 0th index (int i = 0) and will continue till it reaches the end (i < array.Length).

array.Length gives the count of the elements. If we have six elements, the loop will continue till i < 6, the maximum value of the i will be 5 which is the last index of the array. As we know, arrays have a zero-based index system.

Checking For a Target Value in Each Step and Returning a Message Depending on it.

  • array[i] is the ith element in the arrays.

In each step, we check if the current element (array[i]) is equal to our target element, if so our method return "Found".

If it couldn’t have found a target element after searching the whole array, it continues and returns "Not Found".

Calling The Method

As we have written our method, now let’s call it:

We first call the LinearSearch method and stores its return value in a string called found.

Then, we print that string.

Testing Program

Here is the most interesting part, check if the particular number exists in the array or not:

I first check for 26:

It printed "Found". If we remember the array, we can see that we have 26 in it:

Now, let’s check for 7:

It printed "Not Found" as our array doesn’t include the 7.

Conclusion

The Linear search is one of the important algorithms used in programming. If your reading this part, this means that you have read the article. I hope you have understood it.

If you have any question about the article, feel free to share in the comment section.

Thanks for reading my article.

My Other Articles:

--

--