How Does Linear Search Algorithm Work?
In Linear Search Algorithm,
- Every element is considered as a potential match for the key and checked for the same.
- If any element is found equal to the key, the search is successful and the index of that element is returned.
- If no element is found equal to the key, the search yields “No match found”.
For example: Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key = 30
Step 1: Start from the first element (index 0) and compare key with each element (arr[i]).
- Comparing key with first element arr[0]. SInce not equal, the iterator moves to the next element as a potential match.
- Comparing key with next element arr[1]. SInce not equal, the iterator moves to the next element as a potential match.
Step 2: Now when comparing arr[2] with key, the value matches. So the Linear Search Algorithm will yield a successful message and return the index of the element when key is found (here 2).
Introduction to Linear Search Algorithm
Linear Search Algorithm 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. In this article, we will learn about the basics of Linear Search Algorithm, Applications, Advantages, Disadvantages, etc. to provide a deep understanding of Linear Search.
Table of Content
- What is Linear Search Algorithm?
- Algorithm for Linear Search Algorithm
- How Does Linear Search Algorithm Work?
- Implementation of Linear Search Algorithm
- Time and Space Complexity of Linear Search Algorithm
- Applications of Linear Search Algorithm
- Advantages of Linear Search Algorithm
- Disadvantages of Linear Search Algorithm
- When to use Linear Search Algorithm?
- Frequently Asked Questions (FAQs) on Linear Search Algorithm
Contact Us