-
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) · 852 Bytes
/
LinearSearch.java
File metadata and controls
36 lines (29 loc) · 852 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
public class LinearSearch {
public static void main(String[] args) {
int[] nums = {4,3,4,5,4,3,47,37,-34,0,-35,73,2973};
// System.out.println(linearSearch(nums,0));
System.out.println(searchInRange(nums,-35,3,12));
}
static int linearSearch(int[] arr, int target){
if (arr.length == 0){
return -1;
}
for (int i = 0; i < arr.length; i++) {
if(arr[i] == target){
return i;
}
}
return -1;
}
static int searchInRange(int[] arr,int target,int start,int end){
if (arr.length == 0){
return -1;
}
for (int i = start; i <= end; i++) {
if(arr[i] == target){
return i;
}
}
return -1;
}
}