-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathLinearSearch.java
More file actions
52 lines (46 loc) · 1.4 KB
/
LinearSearch.java
File metadata and controls
52 lines (46 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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.
*
* <p>It works for both sorted and unsorted arrays.</p>
*
* <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>
*
* <p>Space Complexity: O(1)</p>
*
* @author Varun Upadhyay
* @author Podshivalov Nikita
* @author Yawar Ali
* @see BinarySearch
* @see SearchAlgorithm
*/
public class LinearSearch implements SearchAlgorithm {
/**
* 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.
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T value) {
if (array == null || array.length == 0 || value == null) {
return -1;
}
for (int i = 0; i < array.length; i++) {
T currentElement = array[i];
if (currentElement != null && currentElement.compareTo(value) == 0) {
return i;
}
}
return -1;
}
}