-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLinearSearch.java
More file actions
38 lines (29 loc) · 1.12 KB
/
Copy pathLinearSearch.java
File metadata and controls
38 lines (29 loc) · 1.12 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
public class LinearSearch {
private LinearSearch(){}
public static <E> int search(E[] data, E target){
for(int i = 0; i < data.length; i ++)
if(data[i].equals(target))
return i;
return -1;
}
public static void main(String[] args){
// int n = 10000;
// Integer[] data = ArrayGenerator.generateOrderedArray(n);
//
// long start = System.currentTimeMillis();
// for (int k = 0; k < 100; k++)
// LinearSearch.search(data, n);
// long time = System.currentTimeMillis() - start;
// System.out.println("n = " + n + " , 100 runs : " + time + "ms");
int[] dataSize = {1000000, 10000000};
for(int n: dataSize) {
Integer[] data = ArrayGenerator.generateOrderedArray(n);
long startTime = System.nanoTime();
for (int k = 0; k < 100; k++)
LinearSearch.search(data, n);
long endTime = System.nanoTime();
double time = (endTime - startTime) / 1000000000.0;
System.out.println("n = " + n + ", 100 runs : " + time + "s");
}
}
}