Skip to content

Instantly share code, notes, and snippets.

@bookybooky
Created March 18, 2017 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bookybooky/d79a868a4ce7a891afcc85e6744ffb72 to your computer and use it in GitHub Desktop.
Save bookybooky/d79a868a4ce7a891afcc85e6744ffb72 to your computer and use it in GitHub Desktop.
sort a deck of card 桶排序
public class SortDeckOfCard {
static class Card {
int suite;
int rank;
public Card(int suite, int rank) {
this.suite = suite;
this.rank = rank;
}
}
public static void sortDeckOfCard(Card[] cards) {
if (cards == null || cards.length == 0) {
return;
}
for (int i = 0; i < cards.length;) {
int index = cards[i].suite * 13 + cards[i].rank;
if (index >= 0 && index < 52
&& (cards[index].suite * 13 + cards[index].rank != index)) {
Card tmp = cards[index];
cards[index] = cards[i];
cards[i] = tmp;
} else {
i++;. From 1point 3acres bbs
}
}
}
public static void main(String[] args) {
Card[] cards = new Card[52];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
cards[51 - i * 13 - j] = new Card(i, j);
}
}
for (int i = 0; i < 52; i++) {
System.out.print((cards[i].suite * 13 + cards[i].rank) + " ");
}
System.out.println();
sortDeckOfCard(cards);
for (int i = 0; i < 52; i++) {
System.out.print((cards[i].suite * 13 + cards[i].rank) + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment