计数排序
2021-03-14 07:37
标签:dex code cache imp rgs next exti main ati 计数排序 标签:dex code cache imp rgs next exti main ati 原文地址:https://blog.51cto.com/tianyiya/2553857import java.util.Arrays;
import java.util.Random;
public class CountSort {
static class Item {
int index;
int num;
}
private static void sort(Item[] items, int bound) {
int n = items.length;
int[] count = new int[bound];
int[] sum = new int[bound];
Item[] cache = Arrays.copyOf(items, n);
for (int i = 0; i = 0; -- i) {
items[-- sum[cache[i].num]] = cache[i];
}
}
public static void main(String[] args) {
int n = 10;
Random random = new Random();
Item[] items = new Item[n];
for (int i = 0; i {
System.out.println(item.index + "->" + item.num);
});
sort(items, 5);
System.out.println("排序后");
Arrays.stream(items).forEach(item -> {
System.out.println(item.index + "->" + item.num);
});
}
}