forked from mfrw/hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_search.java
More file actions
33 lines (27 loc) · 792 Bytes
/
linear_search.java
File metadata and controls
33 lines (27 loc) · 792 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
/**
*
*/
/**
* @author Rajat2712
*
*/
package com.java2novice.algos;
public class MyLinearSearch {
public static int linerSearch(int[] arr, int key){
int size = arr.length;
for(int i=0;i<size;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}
public static void main(String a[]){
int[] arr1= {23,45,21,55,234,1,34,90};
int searchKey = 34;
System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr1, searchKey));
int[] arr2= {123,445,421,595,2134,41,304,190};
searchKey = 421;
System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr2, searchKey));
}
}