Class Search
Search (Algorithmic Structural Variations)
Provides a comparative implementation of fundamental search algorithms (Sequential, Sentinel, and Binary) to demonstrate their operational mechanics and algorithmic complexities.
- Author:
- vicegd
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleansearchBinary(int[] list, int value) Performs a binary search across a strictly sorted array.booleansearchSequential(int[] list, int value) Performs a standard sequential (linear) search across an array.booleansearchSequentialSentinel(List<Integer> list, int value) Performs a sequential search utilizing a "sentinel" value.
-
Constructor Details
-
Search
public Search()
-
-
Method Details
-
searchSequential
public boolean searchSequential(int[] list, int value) Performs a standard sequential (linear) search across an array.Complexity:
O(N)Time |O(1)Space. Evaluates every element sequentially until a match is found or the boundaries are exhausted.- Parameters:
list- The array of integers to be searched.value- The target integer to locate.- Returns:
trueif the element exists within the array,falseotherwise.
-
searchSequentialSentinel
Performs a sequential search utilizing a "sentinel" value.Mechanics: Temporarily appends the target value to the end of the list. This guarantees the search will find the element, eliminating the need to check array boundaries
(i < length)during the inner loop, which can yield slight micro-optimizations in continuous memory blocks.Complexity:
O(N)Time |O(1)Amortized Space.- Parameters:
list- The dynamically resizable list of integers.value- The target integer to locate.- Returns:
trueif the element existed before the sentinel was added.
-
searchBinary
public boolean searchBinary(int[] list, int value) Performs a binary search across a strictly sorted array.Mechanics: Halves the search space on each iteration by comparing the target value against the central pivot. Note: The input array must be sorted prior to execution.
Complexity:
O(log N)Time |O(1)Space.- Parameters:
list- The sorted array of integers.value- The target integer to locate.- Returns:
trueif the element is found,falseotherwise.
-