Linear Search Algorithm
In this article, we will discuss the Linear Search Algorithm. Searching is the process of finding some particular element in the list. If the element is present in the list, then the process is called successful, and the process returns the location of that element; otherwise, the search is called unsuccessful.
Two popular search methods are Linear Search and Binary Search. So, here we will discuss the popular searching technique, i.e., Linear Search Algorithm.
Linear search is also called as sequential search algorithm. It is the simplest searching algorithm. In Linear search, we simply traverse the list completely and match each element of the list with the item whose location is to be found. If the match is found, then the location of the item is returned; otherwise, the algorithm returns NULL.
It is widely used to search an element from the unordered list, i.e., the list in which items are not sorted. The worst-case time complexity of linear search is O(n).
The steps used in the implementation of Linear Search are listed as follows -
Now, let's see the algorithm of linear search.
Working of Linear search
Now, let's see the working of the linear search Algorithm.
To understand the working of linear search algorithm, let's take an unsorted array. It will be easy to understand the working of linear search with an example.
Let the elements of array are -
Let the element to be searched is K = 41
Now, start from the first element and compare K with each element of the array.
The value of K, i.e., 41, is not matched with the first element of the array. So, move to the next element. And follow the same process until the respective element is found.
Now, the element to be searched is found. So algorithm will return the index of the element matched.
Linear Search complexity
Now, let's see the time complexity of linear search in the best case, average case, and worst case. We will also see the space complexity of linear search.
1. Time Complexity
The time complexity of linear search is O(n) because every element in the array is compared only once.
2. Space Complexity
Implementation of Linear Search
Now, let's see the programs of linear search in different programming languages.
Program: Write a program to implement linear search in C language.
Program: Write a program to implement linear search in C++.
Program: Write a program to implement linear search in C#.
Program: Write a program to implement linear search in Java.
Program: Write a program to implement linear search in PHP.
So, that's all about the article. Hope the article will be helpful and informative to you.