-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.java
More file actions
36 lines (29 loc) · 964 Bytes
/
LinearSearch.java
File metadata and controls
36 lines (29 loc) · 964 Bytes
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
package sorting_searching;
import java.util.Comparator;
public class LinearSearch {
public static void main(String[] args) {
// Example:
int[] array = {91, 34, 86, 39, 27, 87, 70, 48, 100, 95};
System.out.println(linearSearch(array, 39)); // 3
System.out.println(linearSearch(array, 40)); // -1 (not found)
}
public static int linearSearch(int[] a, int key) {
for (int i = 0; i < a.length; ++i) {
if (a[i] == key) {
return i;
}
}
return -1;
}
public static <T extends Comparable<? super T>> int linearSearch(T[] a, T key) {
return linearSearch(a, key, Comparator.naturalOrder());
}
public static <T> int linearSearch(T[] a, T key, Comparator<T> comp) {
for (int i = 0; i < a.length; ++i) {
if (comp.compare(a[i], key) == 0) {
return i;
}
}
return -1;
}
}