Javatpoint Logo
Javatpoint Logo

Sequential Search Java

Sequential search, also known as linear search, is a simple searching algorithm used to find a specific target element within a list or an array. The search process involves checking each element in the list one by one until the desired element is found or until the end of the list is reached. Here's an implementation of sequential search in Java:

SequentialSearch.java

Output:

Element found at index: 3

The provided code performs a sequential search on the array arr to find the target element targetElement, which is 15 in this case. The output will indicate whether the element is found and at which index it is located.

In the array arr, the target element 15 is present at index 3. Therefore, the output states that the element is found at index 3.

In this example, we have a class called SequentialSearch containing the sequentialSearch method. The method takes an array arr and a target element target. It then iterates through the array using a for loop and checks each element to see if it matches the target element. If a match is found, the index of the target element is returned.

If no match is found after checking all elements, the method returns -1, indicating that the target element is not present in the array. We call the sequentialSearch() method with these inputs and store the result in the result variable. If the result is not -1, we print the index at which the element was found.

Handling Duplicates and Multiple Occurrences

The basic implementation of sequential search we provided earlier will find the first occurrence of the target element in the array. If there are multiple occurrences of the target element, it will only return the index of the first occurrence. We can modify the method to return a list of indices instead:

SequentialSearch.java

Output:

Element found at indices: [3, 6]

The provided code searches for all occurrences of the target element targetElement (which is 15) in the array arr. It then prints the indices of all occurrences found.

The target element 15 is present at two indices in the array arr - at index 3 and index 6. Therefore, the output displays that the element is found at indices 3 and 6.

Performance and When to Use Sequential Search

Sequential search is straightforward and easy to implement. It's suitable for small arrays or when the array is not sorted. However, it may not be the best choice for large arrays or when frequent searching is required, as its linear time complexity can lead to slow performance.

Time Complexity Analysis

As mentioned earlier, the time complexity of sequential search is O(n) in the worst case, where n is the number of elements in the array. This means that the search time increases linearly with the size of the array. If the array is sorted, there are other search algorithms like binary search with a time complexity of O(log n) that can perform much faster.

Handling Edge Cases and Efficiency Improvements

While the basic sequential search works well for small unsorted arrays, it is essential to consider some edge cases and make efficiency improvements for larger datasets.

1. Handling Empty Array:

In the original implementation, if the array is empty, the search method will still iterate through the loop, even though there are no elements to search. To handle this edge case, we can add a simple check at the beginning of the method:

SequentialSearch.java

Output:

Element found at index: 3

2. Early Termination:

If the target element is found early in the array, we can terminate the search immediately instead of continuing to iterate through the rest of the elements. This can be achieved by modifying the loop condition:

SequentialSearch.java

Output:

Element found at index: 3

3. sing Enhanced For Loop:

In Java, we can also use an enhanced for loop (for-each loop) to simplify the search implementation. This loop iterates through all elements of the array without the need for an explicit index:

SequentialSearch.java

Output:

Element found at index: 3

4. Sorted Array Optimization:

If the array is sorted, we can take advantage of this fact and optimize the search. Instead of using sequential search, which has O(n) time complexity, we can use binary search, which has O(log n) time complexity for sorted arrays.

BinarySearchOptimized.java

Output:

Element found at index: 2

In summary, sequential search is a fundamental algorithm that serves as a starting point for understanding search techniques. By exploring the various optimizations and trade-offs, we gain insights into the importance of algorithmic efficiency and the need to choose the most appropriate algorithm for different scenarios.

It's essential to choose the appropriate search algorithm based on the characteristics of the data and the specific requirements of the application. Sequential search is suitable for small unsorted arrays or when a few searches are needed.

However, for larger arrays or sorted arrays, more advanced search algorithms like binary search should be used to achieve better performance.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA