diff --git a/src/main/java/com/thealgorithms/searches/LinearSearch.java b/src/main/java/com/thealgorithms/searches/LinearSearch.java index c7b70edb5112..c360d47cb10a 100644 --- a/src/main/java/com/thealgorithms/searches/LinearSearch.java +++ b/src/main/java/com/thealgorithms/searches/LinearSearch.java @@ -18,12 +18,42 @@ */ public class LinearSearch implements SearchAlgorithm { - /** - * Generic Linear search method + /** + * Generic Linear search method. * - * @param array List to be searched + *

+ * This method takes an array of elements and a key to search for. + * It traverses the array and compares each element with the key using + * the {@code compareTo()} method. If a match is found, it returns + * the index of the element; otherwise, it returns {@code -1}. + * + *

+ * The linear search algorithm can work on both sorted and unsorted data. + * However, it has a time complexity of O(n) in the worst and average cases, + * as it may need to check every element. + * + *

+ * Note on {@link Comparable}:
+ * The type parameter {@code >} ensures that the elements of + * the array implement the {@link Comparable} interface. This means each element knows + * how to compare itself with another element of the same type using the + * {@code compareTo()} method. + * + *

+ * Example usage: + *

{@code
+     * if (array[i].compareTo(value) == 0) {
+     *     return i;
+     * }
+     * }
+ * The {@code compareTo()} method returns {@code 0} if both elements are equal. + * Using {@code Comparable} allows this algorithm to work with any object type + * (such as Integer, String, or custom classes) that defines its own comparison logic. + * + * @param array Array to be searched * @param value Key being searched for - * @return Location of the key + * @param The type of elements in the array, which must implement Comparable + * @return Index of the key if found, otherwise -1 */ @Override public > int find(T[] array, T value) {