This repository was archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathLinearSearch.java
More file actions
51 lines (45 loc) · 1.76 KB
/
LinearSearch.java
File metadata and controls
51 lines (45 loc) · 1.76 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Algorithm : Linear Search
* Description: Given an array of integers and you need to find a key or a number. If you finds the key in the array, it returns TRUE otherwise FALSE
* Time Complexity : O(n)
*/
import java.util.*;
public class LinearSearch {
private static final String ERROR_STRING = "Please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")";
public static void main(String[] args) {
try {
// Returns TRUE
List<Integer> listOfNumbers = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
Integer keyToSearch = 5;
InputOutputMethod(listOfNumbers, keyToSearch);
// Returns FALSE
keyToSearch = 6;
InputOutputMethod(listOfNumbers, keyToSearch);
// Gives error as no input
listOfNumbers = new ArrayList<Integer>(Arrays.asList());
InputOutputMethod(listOfNumbers, keyToSearch);
}
catch(Exception e) {
System.out.println(ERROR_STRING);
}
}
private static void InputOutputMethod(List<Integer> listOfNumbers, Integer keyToSearch) {
if(listOfNumbers.size() >= 1){
StringBuilder output = new StringBuilder();
Boolean searched = linearSearch(listOfNumbers, keyToSearch);
System.out.println(searched);
}else{
System.out.println(ERROR_STRING);
}
}
private static Boolean linearSearch(List<Integer> list, Integer keyToSearch) {
Boolean answer = false;
for (Integer number : list) {
if(number == keyToSearch) {
answer = true;
break;
}
}
return answer;
}
}