public static void main(String[] args) {
int[] arr=new int[]{56,21,67,45,32,87,34,89,45,74};
simpleCount(arr);
System.out.println(Arrays.toString(arr));
}
public static void simpleCount(int[] arr){
int max=arr[0];
for (int i:arr){
max=Math.max(max,i);
}
int result[] = new int[max+1];//动态初始化
for (int i=0;i<arr.length;i++){
result[arr[i]]++;
}
int index=0;
for (int i=0;i<result.length;i++){
while (result[i]>0){
arr[index++]=i;
result[i]--;
}
}
}
八、堆
public static void main(String[] args) {
int[] arr={1,12,5,8,7,6};
heapSort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
}
private static void heapSort(int[] arr) {
for (int i = (arr.length - 1) / 2; i >= 0; i--) {
adjustHeap(arr, i, arr.length);
}
for (int i = arr.length - 1; i > 0; i--) {
int temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
adjustHeap(arr, 0, i);
}
}
private static void adjustHeap(int[] arr, int parent, int length) {
int temp = arr[parent];
int lChild = 2 * parent + 1;
while (lChild < length) {
int rChild = lChild + 1;
if (rChild < length && arr[lChild] < arr[rChild]) {
lChild++;
}
if (temp >= arr[lChild]) {
break;
}
arr[parent] = arr[lChild];
parent = lChild;
lChild = 2 * lChild + 1;
}
arr[parent] = temp;
}
九 、桶
public static int[] bucketSort( int[] arr) {
int max = arr[0],min = arr[0];// ---- 初始化
for(int val:arr){// -------求出最大最小
max = max>val?max:val;
min = min<val?min:val;
}
int bucketSize = arr.length;//
List<Integer>[] bukcet = new ArrayList[bucketSize+1];
for(int i = 0; i < bukcet.length;i++){
bukcet[i] = new ArrayList<Integer>();
}
for(int val : arr){
int i = (val - min)*bucketSize/(max - min);
bukcet[i].add(val);
}
for(int i = 0; i < bukcet.length;i++){
Collections.sort(bukcet[i]);
}
int k = 0;
for(int i = 0; i < bukcet.length;i++){
for(int j = 0; j < bukcet[i].size();j++)
arr[k++] = bukcet[i].get(j);
}
return arr;
}
public static void main(String[] args) {
int arr[]={18,11,28,45,23,50};
System.out.println(Arrays.toString(bucketSort(arr)));
}
十 、基数
public static void main(String[] args) {
int[] arr={9,10,222,1,223,100,7,50,78};
int maxarr=arr[0];
for (int i:arr ) {
if (maxarr<i){
maxarr=i;
}
}
System.out.println("最大:"+maxarr);
int len=0;
for (int temp=maxarr;temp!=0;temp/=10){
len++;
}
System.out.println("位数:"+len);
arr=sort(arr, len);
System.out.println(Arrays.toString(arr));
}
private static int[] sort(int[] arr,int len){
int sz=10;
int dev=1;
for (int i = 0; i <len ; i++,dev*=10,sz*=10) {
// 考虑负数的情况,这里扩展一倍队列数,其中 [0-9]对应负数,[10-19]对应正数 (bucket + 10)
// int[][] ints = new int[sz*2][0];
int[][] ints = new int[10][0];
for (int j = 0; j <arr.length ; j++) {
// int bucket = ((arr[j]%sz)/dev)+sz;.
int bucket = ((arr[j]%sz)/dev);
System.out.println(arr[j]+"->"+bucket);
ints[bucket]=arrappend(ints[bucket],arr[j]);
}
System.out.println("==========");
int pos = 0;
for (int[] bucket : ints) {
for (int value : bucket) {
arr[pos++] = value;
}
}
}
return arr;
}
private static int[] arrappend(int[] arr,int value){
arr=Arrays.copyOf(arr,arr.length+1);
arr[arr.length-1]=value;
return arr;
}