forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.java
More file actions
141 lines (127 loc) · 4 KB
/
Search.java
File metadata and controls
141 lines (127 loc) · 4 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.util.Arrays;
/**
* Search algorithms for arrays of cards.
*/
public class Search {
/**
* Make an array of 52 cards.
*/
public static Card[] makeDeck() {
Card[] cards = new Card[52];
int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
cards[index] = new Card(rank, suit);
index++;
}
}
return cards;
}
/**
* Displays the given deck of cards.
*/
public static void printDeck(Card[] cards) {
for (int i = 0; i < cards.length; i++) {
System.out.println(cards[i]);
}
}
/**
* Sequential search.
*/
public static int search(Card[] cards, Card target) {
for (int i = 0; i < cards.length; i++) {
if (cards[i].equals(target)) {
return i;
}
}
return -1;
}
/**
* Binary search (iterative version).
*/
public static int binarySearch(Card[] cards, Card target) {
int low = 0;
int high = cards.length - 1;
while (low <= high) {
System.out.println(low + ", " + high);
int mid = (low + high) / 2; // step 1
int comp = cards[mid].compareTo(target);
if (comp == 0) { // step 2
return mid;
} else if (comp < 0) { // step 3
low = mid + 1;
} else { // step 4
high = mid - 1;
}
}
return -1;
}
/**
* Binary search (recursive version).
*/
public static int binarySearch(Card[] cards, Card target,
int low, int high) {
System.out.println(low + ", " + high);
if (high < low) {
return -1;
}
int mid = (low + high) / 2; // step 1
int comp = cards[mid].compareTo(target);
if (comp == 0) { // step 2
return mid;
} else if (comp < 0) { // step 3
return binarySearch(cards, target, mid + 1, high);
} else { // step 4
return binarySearch(cards, target, low, mid - 1);
}
}
/**
* histogram of suits
*/
public static int[] suitHist(Card[] cards) {
int[] hist = new int[4];
for (int i = 0; i < cards.length; i++) {
hist[cards[i].getSuit()]++;
}
return hist;
}
/**
* checks if hand has a flush (>= 5 cards with same suit)
*/
public static boolean hasFlush(Card[] cards) {
int[] hist = suitHist(cards);
for (int i = 0; i < 4; i++) {
if (hist[i] >= 5) {
return true;
}
}
return false;
}
/**
* Demonstrates how to call the search methods.
*/
public static void main(String[] args) {
Card[] cards = new Card[2];
cards[0] = new Card(11, 0);
cards[1] = new Card(15, 1);
System.out.println(hasFlush(cards));
// Card jack = new Card(11, 0);
// Card fake = new Card(15, 1);
//
// System.out.println("Sequential search");
// System.out.println(search(cards, jack));
// System.out.println();
//
// System.out.println("Binary search");
// System.out.println(binarySearch(cards, jack));
// System.out.println();
//
// System.out.println("Failed binary search");
// System.out.println(binarySearch(cards, fake));
// System.out.println();
//
// System.out.println("Recursive binary search");
// System.out.println(binarySearch(cards, jack, 0, 51));
// System.out.println();
}
}