Skip to content

Commit 1ebf8b7

Browse files
authored
Merge pull request #22 from mcgeehancoding/master
Create Linear Search in Java
2 parents baebb55 + b20e4c6 commit 1ebf8b7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Searching/LinearSearch.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class MyLinearSearch {
2+
3+
public static int linerSearch(int[] arr, int key){
4+
5+
int size = arr.length;
6+
for(int i=0;i<size;i++){
7+
if(arr[i] == key){
8+
return i;
9+
}
10+
}
11+
return -1;
12+
}
13+
14+
public static void main(String a[]){
15+
16+
int[] arr1= {23,45,21,55,234,1,34,90};
17+
int searchKey = 34;
18+
System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr1, searchKey));
19+
int[] arr2= {123,445,421,595,2134,41,304,190};
20+
searchKey = 421;
21+
System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr2, searchKey));
22+
}
23+
}

0 commit comments

Comments
 (0)