-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySort.java
More file actions
65 lines (39 loc) · 1.08 KB
/
ArraySort.java
File metadata and controls
65 lines (39 loc) · 1.08 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
import java.util.*;
public class ArraySort {
protected final static int MAX = 4000;
protected final static int NEEDLE = 1999;
int haystack[];
Random r;
public static void main(String[] args) {
ArraySort h = new ArraySort();
if(args.length == 0)
h.play();
else {
int won = 0;
int games = Integer.parseInt(args[0]);
for(int i=0; i< games; i ++)
if (h.play())
++won;
System.out.println("Computer won" + won + " out of " + games + ".");
}
}
public ArraySort() {
haystack = new int[MAX];
r = new Random();
}
public boolean play() {
int i;
for(i=0; i<MAX; i++) {
haystack[i] = (int)(r.nextFloat() * MAX);
}
Arrays.sort(haystack);
i = Arrays.binarySearch(haystack, NEEDLE);
if (i >=0) {
System.out.println("Value " + NEEDLE + "occurs at haystack [" + i + "]" );
return true;
} else {
System.out.println("Value " + NEEDLE + "does not occur in haystack" + haystack[-(i+2)] + " (found at" + -(i+2) + ")");
return false;
}
}
}