import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SortingAndVoting {
private static int getMax(int[] arr) {
return Arrays.stream(arr).max().getAsInt();
}
private static void countSort(int[] arr, int exp) {
int[] output = new int[arr.length];
int[] count = new int[10];
for (int value : arr) {
count[(value / exp) % 10]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = arr.length - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
System.arraycopy(output, 0, arr, 0, arr.length);
}
public static void radixSort(int[] arr) {
int m = getMax(arr);
for (int exp = 1; m / exp > 0; exp *= 10) {
countSort(arr, exp);
}
}
private static boolean checkAndInsert(Map<String, Boolean> votes, String voter) {
if (votes.containsKey(voter)) {
return false;
} else {
votes.put(voter, true);
return true;
}
}
public static void main(String[] args) {
int[] arr = {94, 87, 66, 42, 5, 155, 2, 19};
System.out.println("原始数组: " + Arrays.toString(arr));
radixSort(arr);
System.out.println("排序后的数组: " + Arrays.toString(arr));
Map<String, Boolean> votes = new HashMap<>();
System.out.println("\n投票过程...");
System.out.println("小蛋的投票是" + (checkAndInsert(votes, "小蛋") ? "未重复的" : "重复的"));
System.out.println("小红的投票是" + (checkAndInsert(votes, "小红") ? "未重复的" : "重复的"));
System.out.println("小蛋的第二次投票是" + (checkAndInsert(votes, "小蛋") ? "未重复的" : "重复的"));
}
}