Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/main/java/com/thealgorithms/searches/LinearSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,42 @@
*/
public class LinearSearch implements SearchAlgorithm {

/**
* Generic Linear search method
/**
* Generic Linear search method.
*
* @param array List to be searched
* <p>
* 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}.
*
* <p>
* 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.
*
* <p>
* <b>Note on {@link Comparable}:</b> <br>
* The type parameter {@code <T extends Comparable<T>>} 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.
*
* <p>
* Example usage:
* <pre>{@code
* if (array[i].compareTo(value) == 0) {
* return i;
* }
* }</pre>
* 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 <T> The type of elements in the array, which must implement Comparable
* @return Index of the key if found, otherwise -1
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T value) {
Expand Down