(134 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 |

- How To Write BuzzFizz Program in Java [279 Views]
- Floyd Cycle Detection Algorithm in Java [3830 Views]
- Fetch data from Excel file using Java Spring Web application [3441 Views]
- How to Change Default Tomcat Server Port in Spring Boot [878 Views]
- Tutorial to connect Android device to adb over mobile carrier network - 3G / 4G [1744 Views]

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