-
Notifications
You must be signed in to change notification settings - Fork 40
/
binary.java
77 lines (73 loc) · 2.04 KB
/
binary.java
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
import java.util.*;
import java.util.concurrent.*;
public class binary
{
public static void main(String[] args)
{
long timeToSleep = 1L;
TimeUnit time = TimeUnit.SECONDS;
Random rand = new Random();
// Scanner scan = new Scanner(System.in);
int n = 10000;
System.out.println(n);
int[] C = new int[n];
int[] A = new int[n];
int[] B = new int[n];
for(int i = 0; i < n; ++i)
{
C[i] = rand.nextInt(20000);
}
for(int i = 0; i < n; ++i)
{
A[i] = C[i];
}
//Sorting array A in increasing order using JAVA's built in Arrays.sort()
Arrays.sort(A);
for(int i = 0; i < n; ++i)
{
B[i] = A[n - (i + 1)];
}
// A 9045, 9120, 9300, 9435, 9785
// B 9280, 9375, 9560, 9955 ,10240
// C 9410, 9531, 9686, 10276, 10370
Arrays.sort(C);
int key = rand.nextInt(4000);
long total_time = 0;
long start_time, end_time;
for(int k = 0; k < 30; ++k)
{
start_time = System.nanoTime();
int left = 0;
int right = n - 1;
int mid = (left + right) / 2;
while(left <= right)
{
if(C[mid] == key)
{
break;
}
else if(C[mid] > key)
{
right = mid - 1;
mid = (left + right) / 2;
}
else
{
left = mid + 1;
mid = (left + right) / 2;
}
}
end_time = System.nanoTime();
total_time += (end_time - start_time);
try{
time.sleep(timeToSleep);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
long average_time = total_time / 30;
System.out.printf("%d micro seconds \n", average_time);
}
}