Linear Search with Algorithm and Example

This post was written and made public in order to provide an explanation of a searching method that can be applied when looking for a specific number or item within a list or an array. "Linear search" is the name given to this method of looking things up. In this post, I explained the concept of "linear search" with the assistance of its algorithm and an example. But before we get started, let's first make sure we understand what it is.

What is Linear Search?

Linear search is a method or technique used to search an element from an array.

A very fundamental method known as linear search is utilized when looking for a specific element within a given array.

Because it starts with the very first item in the array and continues to search for the item either until it is found or until the array is finished, it is called a "linear search."

Linear Search Algorithm

The linear search algorithm is as follows:

  • begins with the first index and determines whether the given number is equal to the element present at this (first) index.
  • Return its index number if it is available.
  • Otherwise, keep checking and comparing with the next index.
  • This procedure is repeated until the number is matched.
  • Return -1 if none of the elements match the given number, or print a message such as "The number is not available in the list."

Linear Search Example

For example, let's suppose the elements of the given array arr[10] are 1, 5, 3, 7, 8, 6, 2, 9, 4. These elements get stored in the array arr[] in the following way:

  • 1 is at arr[0]
  • 5 is at arr[1]
  • 3 is at arr[2]
  • and so on.

Then the searching for a number, say "6," proceeds as follows:

  • Checks whether or not 6 is equal to the element at the very first index (arr[0]).
  • 1 is the element present at the very first index.
  • As a result, 6 is compared to 1.
  • because 6 is not equal to 1.
  • Therefore, it compares to the element present at the next index (arr[1]), which is 5.
  • Again, 6 is not equal to 5.
  • Therefore, compare again with the element present at the next index, which is 3.
  • On continuing to search for 6 in the given list
  • It is found at position 5.
  • Return 5 as the index number. That's it.

Programs Created on Linear Search

Computer Fundamentals Quiz


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube