import java.util.HashMap;
/**
* Created by kisstheraik on 16/8/24.
* Description 对扑克牌进行排序,Poker相当于一种字符串,但是两次的桶不一样,一次是13个一次是4个,需要进行两次桶排序
*/
public class PokerSort {
static class Poker{
String suit;
String rank;
public Poker(String suit,String rank){
this.suit=suit;
this.rank=rank;
}
public String toString(){
return "["+suit+" "+rank+"]";
}
//处理桶的关系
public int get(int index){
HashMap<String,Integer> h=new HashMap<>();
h.put("2",1);
h.put("3",2);
h.put("4",3);
h.put("5",4);
h.put("6",5);
h.put("7",6);
h.put("8",7);
h.put("9",8);
h.put("10",9);
h.put("J",10);
h.put("Q",11);
h.put("K",12);
h.put("A",13);
h.put("♣",1);
h.put("♦",2);
h.put("♥",3);
h.put("♠",4);
if(index==0)
return h.get(rank);
else return h.get(suit);
}
}
public static void main(String[] args){
int NUM=30;
Poker[] list={new Poker("♠","A"),new Poker("♥","2"),new Poker("♥","A"),new Poker("♥","3"),new Poker("♦","J"),new Poker("♦","A")};
sort(list);
for (Poker p:
list) {
System.out.print(p.toString()+" ");
}
}
public static void sort(Poker [] list){
int R=14;
int length=list.length;
Poker[] tmp=new Poker[length];
for(int i=0;i<2;i++){
//第一次循环,根据数字排序
int[] count=new int[R+1];
for(int j=0;j<length;j++)
count[list[j].get(i)+1]++;
for(int j=0;j<R;j++)
count[j+1]+=count[j];
for(int j=0;j<length;j++)
tmp[count[list[j].get(i)]++]=list[j];
for(int j=0;j<length;j++)
list[j]=tmp[j];
}
}
}