6
6
*/
7
7
public class Deck {
8
8
9
- // This is a class variable so we don't have to create
10
- // a new Random object every time we call randomInt.
11
- private static Random random = new Random ();
12
-
13
9
private Card [] cards ;
14
10
15
11
/**
@@ -41,65 +37,24 @@ public Card[] getCards() {
41
37
}
42
38
43
39
/**
44
- * Returns a string representation of the deck.
45
- */
46
- public String toString () {
47
- return Arrays .toString (this .cards );
48
- }
49
-
50
- /**
51
- * Swaps the cards at indexes i and j.
40
+ * Displays each of the cards in the deck.
52
41
*/
53
- public void swapCards (int i , int j ) {
54
- Card temp = this .cards [i ];
55
- this .cards [i ] = this .cards [j ];
56
- this .cards [j ] = temp ;
57
- }
58
-
59
- /**
60
- * Chooses a random number between low and high, including both.
61
- */
62
- public int randomInt (int low , int high ) {
63
- int range = high - low + 1 ;
64
- return low + random .nextInt (range );
42
+ public void print () {
43
+ for (int i = 0 ; i < this .cards .length ; i ++) {
44
+ System .out .println (this .cards [i ]);
45
+ }
65
46
}
66
47
67
48
/**
68
49
* Randomly permutes the array of cards.
69
50
*/
70
51
public void shuffle () {
71
- for (int i = 0 ; i < this .cards .length - 1 ; i ++) {
72
- int j = this .randomInt (i , this .cards .length - 1 );
73
- this .swapCards (i , j );
74
- }
75
- }
76
-
77
- /**
78
- * Finds the index of the lowest card
79
- * between low and high inclusive.
80
- */
81
- public int indexLowest (int low , int high ) {
82
- int index = low ;
83
- Card minCard = this .cards [low ];
84
- for (int i = low + 1 ; i <= high ; i ++) {
85
- Card card = this .cards [i ];
86
- if (card .compareTo (minCard ) < 0 ) {
87
- index = i ;
88
- minCard = card ;
89
- }
90
- }
91
- return index ;
92
52
}
93
53
94
54
/**
95
55
* Sorts the cards (in place) using selection sort.
96
56
*/
97
57
public void selectionSort () {
98
- int high = this .cards .length - 1 ;
99
- for (int i = 0 ; i < this .cards .length ; i ++) {
100
- int j = this .indexLowest (i , high );
101
- this .swapCards (i , j );
102
- }
103
58
}
104
59
105
60
/**
@@ -113,85 +68,16 @@ public Deck subdeck(int low, int high) {
113
68
return sub ;
114
69
}
115
70
116
- /**
117
- * Combines two previously sorted subdecks.
118
- */
119
- public static Deck merge (Deck d1 , Deck d2 ) {
120
- Card [] c1 = d1 .cards ;
121
- Card [] c2 = d2 .cards ;
122
- Deck result = new Deck (c1 .length + c2 .length );
123
- Card [] c3 = result .cards ;
124
- int i = 0 ; // index in c1
125
- int j = 0 ; // index in c2
126
-
127
- // for each index in the result
128
- for (int k = 0 ; k < c3 .length ; k ++) {
129
- int choice ;
130
-
131
- // determine which card to merge next
132
- if (i >= c1 .length ) {
133
- choice = 2 ; // c1 is empty
134
- } else if (j >= c2 .length ) {
135
- choice = 1 ; // c2 is empty
136
- } else if (c1 [i ].compareTo (c2 [j ]) < 0 ) {
137
- choice = 1 ; // c1 is lower
138
- } else {
139
- choice = 2 ; // c2 is lower
140
- }
141
-
142
- // store the chosen card in the result
143
- if (choice == 1 ) {
144
- c3 [k ] = c1 [i ];
145
- i ++;
146
- } else {
147
- c3 [k ] = c2 [j ];
148
- j ++;
149
- }
150
- }
151
- return result ;
152
- }
153
-
154
71
/**
155
72
* Returns a sorted copy of the deck using merge sort.
156
73
*/
157
74
public Deck mergeSort () {
158
-
159
- // 0 or 1 cards, already sorted
160
- int len = this .cards .length ;
161
- if (len < 2 ) {
162
- return this ;
163
- }
164
-
165
- // cut the deck about in half
166
- int mid = len / 2 ;
167
- Deck d1 = this .subdeck (0 , mid - 1 );
168
- Deck d2 = this .subdeck (mid , len - 1 );
169
-
170
- // sort each half and merge
171
- d1 = d1 .mergeSort ();
172
- d2 = d2 .mergeSort ();
173
- return merge (d1 , d2 );
75
+ return this ;
174
76
}
175
77
176
78
/**
177
79
* Reorders the cards (in place) using insertion sort.
178
80
*/
179
81
public void insertionSort () {
180
- for (int i = 1 ; i < this .cards .length ; i ++) {
181
- Card card = this .cards [i ];
182
- this .insert (card , i );
183
- }
184
- }
185
-
186
- /**
187
- * Helper method for insertion sort.
188
- */
189
- private void insert (Card card , int i ) {
190
- int j = i ;
191
- while (j > 0 && card .compareTo (this .cards [j - 1 ]) < 0 ) {
192
- this .cards [j ] = this .cards [j - 1 ];
193
- j --;
194
- }
195
- this .cards [j ] = card ;
196
82
}
197
83
}
0 commit comments