From fdda49996486b71d70f29e89bcd92f28eac70b13 Mon Sep 17 00:00:00 2001 From: NajoroRabiaza Date: Thu, 26 Mar 2026 18:03:33 +0300 Subject: [PATCH 1/2] docs: improve LinearSearch Javadoc with steps, example and complexity --- .../thealgorithms/searches/LinearSearch.java | 60 ++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/LinearSearch.java b/src/main/java/com/thealgorithms/searches/LinearSearch.java index 6403749a3154..6110cfb39b78 100644 --- a/src/main/java/com/thealgorithms/searches/LinearSearch.java +++ b/src/main/java/com/thealgorithms/searches/LinearSearch.java @@ -1,47 +1,51 @@ -/** - * Performs Linear Search on an array. - * - * Linear search checks each element one by one until the target is found - * or the array ends. - * - * Example: - * Input: [2, 4, 6, 8], target = 6 - * Output: Index = 2 - * - * Time Complexity: O(n) - * Space Complexity: O(1) - */ package com.thealgorithms.searches; import com.thealgorithms.devutils.searches.SearchAlgorithm; + /** - * Linear Search is a simple searching algorithm that checks - * each element of the array sequentially until the target - * value is found or the array ends. + * LinearSearch - searches for a value in an array by checking each element one by one. + * + * I come from C and this is basically the same as a simple for loop with an if inside. + * The idea is straightforward: start from index 0, compare each element to the target, + * return the index if found, or -1 if we reach the end without finding anything. + * + * How it works step by step: + * 1. We start at the first element (index 0) + * 2. We compare it with the value we are looking for + * 3. If it matches, we return its index + * 4. If not, we move to the next element + * 5. We repeat until we find it or we go through the whole array + * 6. If nothing matched, we return -1 to indicate "not found" * - * It works for both sorted and unsorted arrays. + * Example: + * array = [3, 7, 1, 9, 4], target = 9 + * - index 0: 3 != 9, continue + * - index 1: 7 != 9, continue + * - index 2: 1 != 9, continue + * - index 3: 9 == 9, return 3 + * + * array = [3, 7, 1, 9, 4], target = 5 + * - nothing matched -> return -1 * - * Time Complexity: - * - Best case: O(1) - * - Average case: O(n) - * - Worst case: O(n) + * Time complexity: O(n) because in the worst case we check every element. + * Space complexity: O(1), we do not use any extra memory. * - * Space Complexity: O(1) + * Note: unlike binary search, this works on unsorted arrays too. * * @author Varun Upadhyay * @author Podshivalov Nikita * @see BinarySearch * @see SearchAlgorithm */ - public class LinearSearch implements SearchAlgorithm { /** - * Generic Linear search method + * Goes through the array and returns the index of the target value. + * Returns -1 if the value is not in the array. * - * @param array List to be searched - * @param value Key being searched for - * @return Location of the key + * @param array the array we search in + * @param value the value we are looking for + * @return index of value if found, -1 otherwise */ @Override public > int find(T[] array, T value) { @@ -52,4 +56,4 @@ public > int find(T[] array, T value) { } return -1; } -} +} \ No newline at end of file From b97892aad01e7a53608593264522c3f231388f9d Mon Sep 17 00:00:00 2001 From: NajoroRabiaza Date: Thu, 26 Mar 2026 18:08:57 +0300 Subject: [PATCH 2/2] style: apply clang-format to LinearSearch --- src/main/java/com/thealgorithms/searches/LinearSearch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/LinearSearch.java b/src/main/java/com/thealgorithms/searches/LinearSearch.java index 6110cfb39b78..b612cced0106 100644 --- a/src/main/java/com/thealgorithms/searches/LinearSearch.java +++ b/src/main/java/com/thealgorithms/searches/LinearSearch.java @@ -56,4 +56,4 @@ public > int find(T[] array, T value) { } return -1; } -} \ No newline at end of file +}