(213 Views)

Search algorithms are useful in many areas. One such search algorithm is Linear search. In this piece, you are going to get the complete details about Linear search algorithm in Java.

There are mainly two types of search algorithms including those that don’t make any assumption regarding the order of the list and another one that assumes the list is ordered. Linear search is the simplest search algorithm among them.

Linear search is implemented when you want to find an element in a list. It performs a sequential search on each element of a list till a match is not found or the search of the entire list is done. The return value is considered as the index at which the search term was found or indicator when the search term was not found.

- Best case: When the search terms are in the first slot of the array then the base case occurs. The number of comparisons, in this case, is 1.
- Worst case: It occurs when the search term will be in the last slot of the array, or not available. The number of comparisons, in this case, will be equal to the array size. If an array has N items, then it takes N comparisons in this case.
- Average case: The search term exists in the middle of the array. The number of comparisons, in this case, is N/2.

If you have looked closely, then you will find that the number of comparisons is proportional to the number of items in the array, N in both the worst and average case, the. Therefore, we can say that the number of comparisons is order N in both or O(N) for short. For the best case, the number of comparisons is order 1, or O(1) in short.

- Get the array length
- Choose the element you want to perform search and store it in the variable value
- Make comparison between each element and variable value
- When it match, print a message saying element has been found
- If not, print a message that element not found.

So, isn’t it is easy to use the Linear search algorithm. Anyone can learn it with some practice if he knows to program in any language.

0 UpvotesUpvote |
0 DownvotesDownvote |

- Create a Compressed Zip File in Java [875 Views]
- Factory Design Pattern with Example in Java [2053 Views]
- Difference between Arrays.sort() and Collections.sort() [1182 Views]
- Difference between replace(), replaceAll() and replaceFirst() Method of Java String with Example [1660 Views]
- Difference between Maven and Ant [1010 Views]

- How To Win Ludo King Game Every Time [23510 Views]
- Algorithm to find whether number is Armstrong Number or Not [22752 Views]
- Jio Phone hang on LOGO problem Solution - Hard Reset Jio Phone [15211 Views]
- Knuth-Morris-Pratt (KMP) Substring Search Algorithm with Java Example [14599 Views]
- FlowChart and Algorithm to find Whether a Number is Even or Odd [11424 Views]