-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.java
More file actions
87 lines (81 loc) · 2.84 KB
/
LinearSearch.java
File metadata and controls
87 lines (81 loc) · 2.84 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package module1.introduction.searching;
import java.util.Arrays;
/**
* Lab 1B Linear Search - Mae Morella (2019) Demonstrates a simple linear search
* method.
*/
public class LinearSearch {
/** Performs a linear search and returns the index of the key
*
* @param arr An array of integers to search
* @param key The value being searched for
* @return The first index where key appears. Returns -1 if not found.
*/
public static int linearSearch(int[] arr, int key) {
return linearSearch(arr, key, 0);
}
/** Performs a linear search starting at a certain index.
*
* @param arr An array of integers to search
* @param key The value being searched for
* @param start The index to begin searching at (inclusive)
* @return The first index where key appears. Returns -1 if not found.
*/
public static int linearSearch(int[] arr, int key, int start) {
for (int i = start; i < arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
/** Search an array for a key, and print all results.
* If more than one result, also print the number of results found
* @param arr The array to search
* @param key The value to search for
*/
public static void linearSearchDemo(int[] arr, int key) {
int index = -1;
int foundCount = 0;
while(linearSearch(arr, key, index + 1) != -1) {
index = linearSearch(arr, key, index + 1);
System.out.println("Value " + key + " found at index [" + index + "]");
foundCount++;
}
if (foundCount > 1) {
System.out.println("Value " + key + " was found " + foundCount + " times");
}
if (foundCount == 0) {
System.out.println("Value " + key + " not found in array.");
}
}
/** Generate an array filled with random integer values
* Implementation copied from my response to previous lab module
* @param len The desired length of the random array
* @param min The lower bound of values to use
* @param max The upper bound of values to use (exclusive)
* @return An array of random integers [min, max)
*/
public static int[] randomArray(int len, int min, int max) {
int[] arr = new int[len];
for (int i = 0; i < arr.length; i++) {
double rand = (Math.random()) * (max - min) + min;
arr[i] = (int)rand;
}
return arr;
}
/** Demonstration program */
public static void main(String[] args) {
// nums - demo
int[] nums = new int[]{1, 4, 4, 22, -5, 10, 21, -47, 23};
System.out.println("nums[]: " + Arrays.toString(nums));
System.out.println("linearSearchDemo(nums, 10): ");
linearSearchDemo(nums, 10);
System.out.println();
// random array - demo
int[] data = randomArray(20, 0, 20);
System.out.println("data[]: " + Arrays.toString(data));
System.out.println("linearSearchDemo(data, 10): ");
linearSearchDemo(data, 10);
}
}