Hello guys, earlier, I have talked about how the binary search algorithm works and shared the code to implement the binary search in Java. In that article, someone asked me about is there any other search algorithm exists? How can you search an element in the array if it’s not sorted, and you cannot use the binary search algorithm? To answer his questions, I mentioned about the Linear search algorithm, which is the predecessor of binary search. Generally, it is taught before the binary search algorithm because the binary search is faster than Linear search. However, nevermind, you can still learn this useful algorithm to search an item in the array or linked list.
Linear search or sequential search is a method for finding a particular value in a list that consists of checking every one of its elements, one at a time and in sequence until the desired one is found.
The Linear search algorithm is the most straightforward. For a list of n items, the best case is when the value is equal to the first element of the list, in which case only one comparison is needed. The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed.
The worst-case performance scenario for a linear search is that it has to loop through the entire collection, either because the item is the last one, or because the item is not found.
In other words, if you have N items in your collection, the worst-case scenario to find a topic is N iterations. In Big O Notation it is O(N). The speed of search grows linearly with the number of items within your collection. Unlike the Binary Search algorithm, Linear searches don’t require the collection to be sorted.
Btw, if you are not familiar with the essential data structure and algorithms like this one, it’s better to first go through a suitable data structure and algorithm course like Data Structures and Algorithms: Deep Dive Using Java. This is a comprehensive resource to learn fundamental data structures and algorithms in Java programming languages. It’s also very affordable, and you can buy in just $10 on Udemy’s monthly sale.
Java Program to perform Linear Search
Here is our sample program to implement a sequential search algorithm in Java. It’s self-explanatory, but if you have any doubt in understanding any part of the code then please shout and I would be happy to clear any doubt you have.
You can also read the Grokking Algorithms book, one of my favorite books to learn fundamentals Data Structure and Algorithms. It has a whole chapter on the liner and binary search and here is a diagram which neatly explains the difference between linear and binary search algorithm.
You can see how the linear search algorithm because slower and slower as the size of the array or number of elements increases.
That’s all about
how to implement a linear search algorithm in Java. It is one of the first search algorithms you should learn in your computer science class. Teachers and Professors explain binary search next, but you have already learned that. Nevermind, we have a lot of sorting algorithms that you can explore after this, and the following article will help you.
If you are preparing for interviews and ramping up your Data structure and algorithms skill, you can also take a look at following resources to take your preparation next level:
earching and Sorting algorithm tutorial you may like
- How to implement the insertion sort algorithm in Java? (tutorial)
- How to apply the Quicksort algorithm in place in Java? (tutorial)
- How to implement the Bubble sort algorithm in Java? (tutorial)
- Difference between Comparison and Non-Comparison based sorting algorithm? (answer)
- How to apply Bucket Sort in Java? (tutorial)
- How to implement Quicksort algorithm without recursion? (tutorial)
- How to perform a Binary Search Algorithm in Java? (tutorial)
- How to find all pairs in an array whose sum is equal to k (solution)
- How to remove duplicates from an array in Java? (solution)
- How to find the most significant and smallest number in an array without sorting? (solution)
- How to find duplicates from an unsorted array in Java? (solution)
- How to find one missing number in a sorted array? (solution)
- How to find a missing value from an array containing 1 to 100? (solution)
- How to count the number of leaf nodes in a given binary tree in Java? (solution)
- Recursive InOrder traversal Algorithm (solution)
- 50+ Data Structure and Algorithms Problems from Interviews (questions)
- My favorite free courses to learn data Structure in-depth (FreeCodeCamp)
- How to remove an element from an array in Java? (solution)
- How to check if an array contains a particular value? (solution)
- Iterative PreOrder traversal in a binary tree (solution)
- 10 Free Data Structure and Algorithm Courses for Programmers (courses)
- 100+ Data Structure Coding Problems from Interviews (questions)
Thanks for reading this article. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.
P. S. – If you are looking for some Free Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check the
Easy to Advanced Data Structures course on Udemy. It’s authored by a Google Software Engineer and Algorithm expert, and it’s completely free of cost.
Published on Java Code Geeks with permission by Javin Paul, partner at our JCG program. See the original article here: How Linear Search or Sequential Search Algorithms works in Java? Example Tutorial
Opinions expressed by Java Code Geeks contributors are their own.