3
3
*/
4
4
public class Search {
5
5
6
+ /**
7
+ * Make an array of 52 cards.
8
+ */
9
+ public static Card [] makeDeck () {
10
+ Card [] cards = new Card [52 ];
11
+ int index = 0 ;
12
+ for (int suit = 0 ; suit <= 3 ; suit ++) {
13
+ for (int rank = 1 ; rank <= 13 ; rank ++) {
14
+ cards [index ] = new Card (rank , suit );
15
+ index ++;
16
+ }
17
+ }
18
+ return cards ;
19
+ }
20
+
21
+ /**
22
+ * Displays the given deck of cards.
23
+ */
24
+ public static void printDeck (Card [] cards ) {
25
+ for (int i = 0 ; i < cards .length ; i ++) {
26
+ System .out .println (cards [i ]);
27
+ }
28
+ }
29
+
6
30
/**
7
31
* Sequential search.
8
32
*/
@@ -24,14 +48,14 @@ public static int binarySearch(Card[] cards, Card target) {
24
48
while (low <= high ) {
25
49
System .out .println (low + ", " + high );
26
50
27
- int mid = (low + high ) / 2 ; // step 1
51
+ int mid = (low + high ) / 2 ; // step 1
28
52
int comp = cards [mid ].compareTo (target );
29
53
30
- if (comp == 0 ) { // step 2
54
+ if (comp == 0 ) { // step 2
31
55
return mid ;
32
- } else if (comp < 0 ) { // step 3
56
+ } else if (comp < 0 ) { // step 3
33
57
low = mid + 1 ;
34
- } else { // step 4
58
+ } else { // step 4
35
59
high = mid - 1 ;
36
60
}
37
61
}
@@ -41,40 +65,25 @@ public static int binarySearch(Card[] cards, Card target) {
41
65
/**
42
66
* Binary search (recursive version).
43
67
*/
44
- public static int binarySearchRec (Card [] cards , Card target ,
45
- int low , int high ) {
68
+ public static int binarySearch (Card [] cards , Card target ,
69
+ int low , int high ) {
46
70
System .out .println (low + ", " + high );
47
71
48
72
if (high < low ) {
49
73
return -1 ;
50
74
}
51
- int mid = (low + high ) / 2 ; // step 1
75
+ int mid = (low + high ) / 2 ; // step 1
52
76
int comp = cards [mid ].compareTo (target );
53
77
54
- if (comp == 0 ) { // step 2
78
+ if (comp == 0 ) { // step 2
55
79
return mid ;
56
- } else if (comp < 0 ) { // step 3
57
- return binarySearchRec (cards , target , mid + 1 , high );
58
- } else { // step 4
59
- return binarySearchRec (cards , target , low , mid - 1 );
80
+ } else if (comp < 0 ) { // step 3
81
+ return binarySearch (cards , target , mid + 1 , high );
82
+ } else { // step 4
83
+ return binarySearch (cards , target , low , mid - 1 );
60
84
}
61
85
}
62
86
63
- /**
64
- * Make an array of 52 cards.
65
- */
66
- public static Card [] makeDeck () {
67
- Card [] cards = new Card [52 ];
68
- int index = 0 ;
69
- for (int suit = 0 ; suit <= 3 ; suit ++) {
70
- for (int rank = 1 ; rank <= 13 ; rank ++) {
71
- cards [index ] = new Card (rank , suit );
72
- index ++;
73
- }
74
- }
75
- return cards ;
76
- }
77
-
78
87
/**
79
88
* Demonstrates how to call the search methods.
80
89
*/
@@ -96,7 +105,8 @@ public static void main(String[] args) {
96
105
System .out .println ();
97
106
98
107
System .out .println ("Recursive binary search" );
99
- System .out .println (binarySearch (cards , jack ));
108
+ System .out .println (binarySearch (cards , jack , 0 , 51 ));
100
109
System .out .println ();
101
110
}
111
+
102
112
}
0 commit comments