forked from MakeContributions/DSA
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear-search.java
More file actions
Latest commit
21 lines (19 loc) · 819 Bytes
/
Copy pathlinear-search.java
File metadata and controls
21 lines (19 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Linear search to check for a given key in an array
publicclassMyLinearSearch{
publicstaticintlinearSearch(int[] arr, intkey){
for(inti=0;i<arr.length;i++){
if(arr[i] == key){
returni;
}
}
return -1;
}
publicstaticvoidmain(Stringargs[]){
int[] arr1= {10,20,30,50,70,90};
intkey = 30;
System.out.println(key+" is present and is found at index: "+linearSearch(arr1, key));
}
}
// For running in terminal rename this file to MyLinearSearch.java
//then run the commands <javac MyLinearSearch.java> followed by <java MyLinearSearch>
//It will generate and a MyLinearSearch.class file which is a file containing java bytecode that is executed by JVM.