11import java .util .Arrays ;
22import java .util .Random ;
33
4+
45/**
56 * A deck of playing cards (of fixed size).
67 */
78public class Deck {
89
910 private Card [] cards ;
11+ private Random random = new Random ();
1012
1113 /**
1214 * Constructs a standard deck of 52 cards.
@@ -49,40 +51,71 @@ public void print() {
4951 * Returns a string representation of the deck.
5052 */
5153 public String toString () {
52- return Arrays .toString (this .cards );
54+ StringBuilder output = new StringBuilder ();
55+ for (Card card : cards ) {
56+ output .append (card .toString () + "\n " );
57+ }
58+ return output .toString ();
59+ //return Arrays.toString(this.cards);
5360 }
5461
5562 /**
5663 * Chooses a random number between low and high, including both.
5764 */
5865 public int randomInt (int low , int high ) {
59- return 0 ;
66+ if (high < low ) {
67+ int placeholder = low ;
68+ low = high ;
69+ high = placeholder ;
70+ }
71+ int rand = random .nextInt (high - low + 1 );
72+ rand += low ;
73+ return rand ;
6074 }
6175
6276 /**
6377 * Swaps the cards at indexes i and j.
6478 */
6579 public void swapCards (int i , int j ) {
80+ Card card1 = cards [i ];
81+ cards [i ] = cards [j ];
82+ cards [j ] = card1 ;
6683 }
6784
6885 /**
6986 * Randomly permutes the array of cards.
7087 */
7188 public void shuffle () {
89+ for (int i = 0 ; i < cards .length ; i ++) {
90+ int j = randomInt (i , cards .length - 1 );
91+ swapCards (i , j );
92+ }
7293 }
7394
7495 /**
7596 * Finds the index of the lowest card
7697 * between low and high inclusive.
7798 */
7899 public int indexLowest (int low , int high ) {
79- return 0 ;
100+ int min = low ;
101+ for (int i = low +1 ; i <= high ; i ++) {
102+ if (cards [i ].compareTo (cards [min ]) == -1 ) {
103+ min = i ;
104+ }
105+ }
106+ return min ;
80107 }
81108
82109 /**
83110 * Sorts the cards (in place) using selection sort.
84111 */
85112 public void selectionSort () {
113+ int j ;
114+ for (int i = 0 ; i < cards .length ; i ++) {
115+ if ((j = indexLowest (i , cards .length -1 )) >i ) {
116+ swapCards (i , j );
117+ }
118+ }
86119 }
87120
88121 /**
@@ -100,20 +133,63 @@ public Deck subdeck(int low, int high) {
100133 * Combines two previously sorted subdecks.
101134 */
102135 public static Deck merge (Deck d1 , Deck d2 ) {
103- return null ;
136+ int fullDeck = d1 .cards .length + d2 .cards .length ;
137+ Deck combine = new Deck (fullDeck );
138+ int i = 0 ;
139+ int j = 0 ;
140+ for (int k = 0 ; k < fullDeck ; k ++) {
141+ if (i >= d1 .cards .length ) {
142+ combine .cards [k ] = d2 .cards [j ];
143+ j ++;
144+ } else if (j >= d2 .cards .length ) {
145+ combine .cards [k ] = d1 .cards [i ];
146+ i ++;
147+ } else if (d1 .cards [i ].compareTo (d2 .cards [j ]) == 1 ) { // d2 < d1
148+ combine .cards [k ] = d2 .cards [j ];
149+ j ++;
150+ } else if (d1 .cards [i ].compareTo (d2 .cards [j ]) == -1 ) { // d1 < d2
151+ combine .cards [k ] = d1 .cards [i ];
152+ i ++;
153+ }
154+ else { // to avoid freezing i and j if the cards are somehow duplicates
155+ combine .cards [k ] = d1 .cards [i ];
156+ i ++;
157+ }
158+ }
159+
160+ return combine ;
104161 }
105162
106163 /**
107164 * Returns a sorted copy of the deck using merge sort.
108165 */
109166 public Deck mergeSort () {
110- return this ;
167+ if (cards .length <= 1 ) {
168+ return this ;
169+ }
170+ Deck d1 = subdeck (0 , cards .length / 2 - 1 );
171+ Deck d2 = subdeck (cards .length / 2 , cards .length - 1 );
172+ d1 = d1 .mergeSort ();
173+ d2 = d2 .mergeSort ();
174+ return merge (d1 , d2 );
111175 }
112176
113177 /**
114178 * Reorders the cards (in place) using insertion sort.
115179 */
116180 public void insertionSort () {
181+ for (int i = 1 ; i < cards .length ; i ++) {
182+ for (int k = i ; k > 0 ; k --) {
183+ if (cards [k ].compareTo (cards [k -1 ]) == -1 ) {
184+ swapCards (k , k -1 );
185+ }
186+ }
187+ }
188+ }
189+
190+ public static void main (String [] args ) {
191+ Deck deck = new Deck ();
192+ System .out .println (deck );
117193 }
118194
119195}
0 commit comments