blob: cfb89edea08e8ba7c500139e7a597a52c18cd49b (
plain) (
blame)
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
|
class BinaryVsLinear
{
private static int linearSearch(int key, int[] keys)
{
int searchCount = 0;
for (int i=0; i<keys.length; i+=1) {
searchCount += 1;
if (keys[i]==key) {
return searchCount;
}
} return -1;
}
private static int binarySearch(int key, int[] keys)
{
int left=0; int right=(keys.length-1); int mid=0;
int searchCount=0;
while (true) {
if (left>right) {
mid=-1;
break;
} else {
mid=(left+right)/2;
searchCount += 2;
if (key<keys[mid]) {
right=mid-1;
}
else if (key>keys[mid]) {
left=mid+1;
} else {
break;
}
}
} return searchCount;
}
public static void main(String[] args)
{
for (int length = 1; length <= 30; length += 1)
{
int[] array = new int[length];
for (int index = 0; index < length; index += 1)
{
array[index] = index;
}
double linearTotal = 0.0;
double binaryTotal = 0.0;
for (int element = 0; element < length; element += 1)
{
linearTotal += linearSearch(element, array);
binaryTotal += binarySearch(element, array);
}
double linearAverage = linearTotal / length;
double binaryAverage = binaryTotal / length;
System.out.println(length + " " + linearAverage + " " + binaryAverage);
}
}
}
|