diff --git a/src/main/java/com/thealgorithms/searches/LinearSearch.java b/src/main/java/com/thealgorithms/searches/LinearSearch.java index bd14fe21ea03..c5f6e6ba9776 100644 --- a/src/main/java/com/thealgorithms/searches/LinearSearch.java +++ b/src/main/java/com/thealgorithms/searches/LinearSearch.java @@ -10,10 +10,34 @@ * * It works for both sorted and unsorted arrays. * + *
How it works step-by-step: + *
Example: + *
+ * Input array: [5, 3, 8, 1, 9] + * Target: 8 + * + * Step 1: Compare 5 with 8 → no match, move on + * Step 2: Compare 3 with 8 → no match, move on + * Step 3: Compare 8 with 8 → match found at index 2! + * + * Output: 2 + * + * If target = 7: + * Output: -1 (not found) + ** Time Complexity: - * - Best case: O(1) - * - Average case: O(n) - * - Worst case: O(n) + * - Best case: O(1) - target is the first element + * - Average case: O(n) - target is somewhere in the middle + * - Worst case: O(n) - target is last or not present * * Space Complexity: O(1) * @@ -25,9 +49,10 @@ public class LinearSearch implements SearchAlgorithm { /** - * Generic Linear search method + * Generic Linear search method that searches for a value + * in the given array by checking each element one by one. * - * @param array List to be searched + * @param array List to be searched (can be unsorted) * @param value Key being searched for * @return Location of the key, -1 if array is null or empty, or key not found */