import java.util.*;
public class Dealer {
public static void main(String[] args){
ArrayList<Card> cards = new ArrayList<Card>();
for (Suits s : Suits.values()) {
for (Ranks r : Ranks.values()) {
Card value = new Card(s,r);
cards.add(value);
}
}
shuffle(cards);
present(cards);
System.out.println();
cards = selectionSort(cards);
present(cards);
}
private static void shuffle(ArrayList<Card> list){
Collections.shuffle(list);
}
private static void present(ArrayList<Card> list){
int count = 0;
for (Card c : list){
count++;
System.out.println(c.toString());
}
}
private static ArrayList<Card> selectionSort(ArrayList<Card> list){
int min;
int count = 0;
for (int i = 0; i < list.size(); i++) {
count++;
// Assume first element is min
min = i;
for (int j = i + 1; j < list.size(); j++) {
if (list.get(j).Get() < list.get(min).Get()) {
min = j;
}
}
if(min != i){
final Card temp = list.get(i);
list.set(i,list.get(min));
list.set(min,temp);
}
// System.out.println(list.get(i));// I print the in ascending order
}
System.out.println("================================== Sorted Deck Below =========================================================");
System.out.println("It took "+count+" iterations to sort this deck using selection sort.");
System.out.println();
return list;
}
private static ArrayList<Card> InsertionSort(ArrayList<Card> list){
int count = 0;
for (int i = 1; i < list.size(); i++) {
Card temp = list.get(i);
int j;
for(j = i-1;j>=0&&temp.Get()<list.get(j).Get(); j--){
list.set(j+1,list.get(j));
}
list.set(j+1,temp);
count ++;
}
System.out.println("================================== Sorted Deck Below =========================================================");
System.out.println("It took "+count+" iterations to sort this deck using insertion sort.");
System.out.println();
return list;
}
//mergeSort code below this is where I get confused on its implementation
public static void mergeSort(ArrayList<Card> list){
if (list.size() ==1){
return list;
}
else {
int mid= list.size()/2;
ArrayList<Card> left= new ArrayList<Card>(mid);
ArrayList<Card> right=new ArrayList<Card>(list.size()-mid);
for (int i = 0; i < mid; i++) {
left.add(list.get(i));
}
for (int i = 0; i < list.size()-mid; i++) {
right.add(list.get(i+mid));
}
left=mergeSort(left);
right=mergeSort(right);
merge(left,right,list);
}
return list;
}
public static void merge (ArrayList<Card>left,ArrayList<Card>right,ArrayList<Card>list){
int i1=0;// left Index
int i2=0;// right Index
int ListIndex=0;
for (int i = 0; i < list.size(); i++) {
if (i2>=right.size() || (i1<left.size() && (right.get(i)>=left.get(i))))
{
list.set(ListIndex,left.get(i1));
ListIndex++;
}
else {
list.set(ListIndex, right.get(i2));
ListIndex++;
}
}
}
}
=============================================== ============================
// Created enums
enum Ranks{
Ace,King,Queen,Jack,Ten,Nine,Eight,Seven,Six,Five,Four,Three,Two
}
enum Suits{
Spades,
Hearts,
Diamonds,
Clubs
}
public class Card{
private Ranks rank;
private Cards card;
public Card(Suits suit, Ranks rank){
this.suit = suit;
this.rank = rank;
}
public int Get(){
return (suit.ordinal())+rank.ordinal()*4;
}
public String toString(){
return rank.toString()+" of " +suit.toString();
}
}
我是使用ArrayList进行排序的初学者,但后来我看到这个代码有SelectionSort和InsertionSort然后我认为如果那两个在那里,那么我可以让MergeSort使用ArrayList做同样的事情。我理解它实际上会做什么但我对如何使用ArrayList实现它感到困惑。我在设置这些方法以处理ArrayList以使我的数组排序时遇到了麻烦。有谁知道我应该尝试纠正这个问题?我可以在这段代码的某些方面告诉我,很明显可以告诉某些部分会出现问题。为了澄清,我的主要问题是我如何能够为MergeSort方法实现ArrayList以正确地允许MergeSort对卡片进行排序?正如你所看到的那样,我确实尝试将ArrayList放在方法中,但我还没有真正成功地使用它。