Class SequentialSearch
java.lang.Object
topics.divideconquer.search.SequentialSearch
Sequential (Linear) Search
Implements the baseline linear search paradigm to locate an element within an array. Unlike Binary Search, Sequential Search makes no assumptions about data layout and functions correctly on both sorted and unsorted collections.
Divide invalid input: '&' Conquer Strategy by Subtraction
The recursive approach demonstrates a Divide and Conquer by Subtraction model
(where a = 1, b = 1, k = 0). Each step safely isolates the head element
for evaluation and reduces the subsequent search space dimension by exactly 1.
Complexity Analysis
- Time Complexity:
O(N)- Worst/Average case requires scanning all elements.O(1)Best case (element at index 0). - Space Complexity (Iterative):
O(1)- Requires constant auxiliary index memory. - Space Complexity (Recursive):
O(N)- Bound directly by call stack frames depth.
- Author:
- vicegd
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintsearchIterative(int[] v, int x) Iterative implementation of sequential linear search.intsearchRecursive(int[] v, int x) Recursive implementation of sequential linear search.
-
Constructor Details
-
SequentialSearch
public SequentialSearch()
-
-
Method Details
-
searchIterative
public int searchIterative(int[] v, int x) Iterative implementation of sequential linear search. Generally preferred over the recursive counter-part due to memory constraints.- Parameters:
v- The target array to search across (can be unsorted).x- The target value to locate.- Returns:
- The absolute index of target value x within v, or Integer.MIN_VALUE if not found.
-
searchRecursive
public int searchRecursive(int[] v, int x) Recursive implementation of sequential linear search. Illustrates Divide and Conquer by Subtraction semantics.- Parameters:
v- The target array to search across (can be unsorted).x- The target value to locate.- Returns:
- The absolute index of target value x within v, or Integer.MIN_VALUE if not found.
-