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
44 changes: 17 additions & 27 deletions src/main/java/com/thealgorithms/searches/LinearSearch.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
/**
* 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.
* 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.
*
* It works for both sorted and unsorted arrays.
* <p>It works for both sorted and unsorted arrays.</p>
*
* Time Complexity:
* - Best case: O(1)
* - Average case: O(n)
* - Worst case: O(n)
* <p>Time Complexity:
* <ul>
* <li>Best case: O(1) - Target is at the first index.</li>
* <li>Average case: O(n) - Target is in the middle.</li>
* <li>Worst case: O(n) - Target is at the end or missing.</li>
* </ul>
* </p>
*
* Space Complexity: O(1)
* <p>Space Complexity: O(1)</p>
*
* @author Varun Upadhyay
* @author Podshivalov Nikita
* @author yawarali1
* @see BinarySearch
* @see SearchAlgorithm
*/
public class LinearSearch implements SearchAlgorithm {

/**
* Generic Linear search method
* Generic Linear search method.
*
* @param array List to be searched
* @param value Key being searched for
* @return Location of the key, -1 if array is null or empty, or key not found
* @param array List to be searched.
* @param value Key being searched for.
* @return Location of the key, -1 if array is null or empty, or key not found.
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T value) {
Expand All @@ -59,4 +49,4 @@ public <T extends Comparable<T>> int find(T[] array, T value) {

return -1;
}
}
}
Loading