Created
March 18, 2017 05:20
-
-
Save bookybooky/d79a868a4ce7a891afcc85e6744ffb72 to your computer and use it in GitHub Desktop.
sort a deck of card 桶排序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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