-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_search.java
More file actions
28 lines (26 loc) · 992 Bytes
/
Copy pathLinear_search.java
File metadata and controls
28 lines (26 loc) · 992 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
import java.util.Arrays;
public class Linear_search {
public static void main(String[] args) {
int nums[] = {23, 46, 43, 78, 33, 90, 11, 39};
int target = 11;
int ans = Linear_search(nums, target);
System.out.println("Array: " + Arrays.toString(nums));
System.out.println("Target " + target + " found at index: " + ans);
}
// Search in the array: return the index if the item is found,
// otherwise return -1 indicating item not found
static int Linear_search(int arr[], int target) {
if (arr.length == 0) {
return -1;
}
for (int index = 0; index < arr.length; index++) {
// Check if the element at the current index matches the target
int element = arr[index];
if (element == target) {
return index;
}
}
// Return -1 if the target was not found in the array
return -1;
}
}