Skip to content

Commit db39c54

Browse files
committed
Implement 'Linear Search' function in Java
1 parent 1317f7a commit db39c54

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

linear_search/LinearSearch.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
/**
4+
* LinearSearch
5+
*/
6+
public class LinearSearch {
7+
8+
public static void main(String[] args) {
9+
Integer inputArray[] = {13, 5, 17, 2, 11, 35};
10+
Integer targetNum = 11;
11+
boolean targetFoundFlg = false;
12+
13+
long start = System.nanoTime();
14+
15+
for(Integer inputNum : inputArray) {
16+
if(targetNum == inputNum) {
17+
targetFoundFlg = true;
18+
System.out.println("The target number(" + targetNum + ") was found.");
19+
break;
20+
}
21+
}
22+
23+
if(targetFoundFlg != true) {
24+
System.out.println("The target number (" + targetNum + ") was not found...");
25+
}
26+
27+
long end = System.nanoTime();
28+
29+
System.out.println("Exec time: " + (end - start) / 1000000f + " ms");
30+
}
31+
}

0 commit comments

Comments
 (0)