Linear Search Algorithm - David-Chae/Algorithms_Notes_Solutions GitHub Wiki

What is Linear Search

Linear Search is defined as a sequential search algorithm that starts at one end and goes through each element of a list until the desired element is found, otherwise the search continues till the end of the data set. It is the easiest searching algorithm.

Linear Search

Given an array arr[] of N elements, the task is to write a function to search a given element x in arr[].

Follow the below idea to solve the problem:

Iterate from 0 to N-1 and compare the value of every index with x if they match return index

Follow the given steps to solve the problem:

Start from the leftmost element of arr[] and one by one compare x with each element of arr[] If x matches with an element, return the index. If x doesn’t match with any of the elements, return -1. Below is the implementation of the above approach:

Linear Search Algorithm - Java Implementation

Recursive Linear Search Algorithm - Java Implementation

Recursive Linear Search Algorithm - Python Implementation.

Time complexity: O(N) Auxiliary Space: O(1)

Reference:

https://www.geeksforgeeks.org/linear-search/