-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheLinearSearch.java
More file actions
30 lines (26 loc) · 949 Bytes
/
Copy pathTheLinearSearch.java
File metadata and controls
30 lines (26 loc) · 949 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
// Linear Search : sequentially checks each element until a match is found or the entire
// list has been examined.
// Runtime complexity : O(n)
// Slow for large data sets
// Does not need to sorted
// Useful for DS that don't have random access (Linked List)
public class TheLinearSearch {
public static void main(String[] args) {
int[] array = { 9, 1, 8, 7, 5, 2, 0 };
int index = linearSearch(array, 9);
if (index != -1) {
System.out.println("Element found at index: " + index);
} else {
System.out.println("Element not found");
}
}
private static int linearSearch(int[] array, int value) {
for (int i = 0; i < array.length; i++) {
// Find the index
if (array[i] == value) {
return i;
}
}
return -1;
}
}