forked from DhanushNehru/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.java
More file actions
20 lines (17 loc) · 784 Bytes
/
LinearSearch.java
File metadata and controls
20 lines (17 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class LinearSearch {
// Declare method to perform the search, taking an array and searchFor as params
public static int linearSearch(int[] arr, int searchFor) {
// Linear search: Iterates through an array until the value is found or the array is fully traversed.
// Time Complexity: Best - O(1) Average - O(n) Worst - O(n)
// NOTE: Best case is when the value is in the first index.
// Iterate over array until fully traversed
for (int i = 0; i < arr.length; i++) {
// If the value at the current index is the searchFor value, return the index
if (arr[i] == searchFor) {
return i;
}
}
// If the value is not found, return -1
return -1;
}
}