计数排序

package com.sort;

//计数排序:O(n)
public class CountSort {
	//适用于排序元素在一个数值范围内
	//一下以公司员工年龄为元素排序(Age:18-70)
	public static void main(String[] args) {
		int[] a = new int[]{26,25,24,23,23,26,25,24,23,26,25,24,33,33,45,55,56,19,23,23,23,22,23,21};
		int[] count = countSort(a);
		for(int i=18; i<71; i++){
			int counts = count[i];
			while(counts-- != 0 ){
				System.out.println(i);
			}
		}
	}

	private static int[] countSort(int[] a) {
		int[] count = new int[71];
		for(int elem:a){
			count[elem]++;
		}
		return count;
	}
}

你可能感兴趣的:(计数排序)