计数排序(Counting-Sort)
2017-11-11 12:48
417 查看
计数排序(Counting-Sort)
计数排序假设n个输入元素的每一个都是在0到k闭区间的一个整数,它的时间复杂度为O(n),下面是计数排序的Java实现:import java.util.Arrays; /** * Created by CvShrimp on 2017/11/11. */ public class CountingSort { public static int[] countingSort(int[] array, int k) { //k表示待排序数组的最大的元素值 int[] c = new int[k + 1]; for(int j = 0; j < array.length; j++) { //c[i]记录原数组中等于i的元素的个数 c[array[j]] = c[array[j]] + 1; } for(int i = 1; i <= k; i++) { //c[i]记录原数组中小于等于i的元素个数 c[i] = c[i] + c[i - 1]; } int[] b = new int[array.length]; for(int j = array.length - 1; j >= 0; j--) { b[c[array[j]] - 1] = array[j]; c[array[j]] = c[array[j]] - 1; } return b; } public static void main(String[] args) { int[] array = {6,5,6,66,1,10,33}; int[] sortedArray = CountingSort.countingSort(array, 66); System.out.println(Arrays.toString(sortedArray)); } }
执行结果如下所示:
[1, 5, 6, 6, 10, 33, 66] Process finished with exit code 0
相关文章推荐
- 计数排序(Counting Sort)
- 【CLRS】《算法导论》读书笔记(三):计数排序(Counting sort)、基数排序(Radix sort)和桶排序(Bucket sort)
- 计数排序(Counting Sort)
- 【算法导论学习-014】计数排序(CountingSortTest)
- 排序算法系列:计数排序(Counting sort)(C语言)
- 线性排序:计数排序 Counting Sort 和 基数排序 Radix Sort
- 计数排序(counting-sort)——算法导论(9)
- CountingSort -- 计数排序(C++)
- 计数排序(Counting Sort)、桶排序(Bucket Sort)和基数排序(Radix Sort)
- Counting sort 计数排序
- 计数排序(Counting Sort)
- 计数排序源码(counting sort)
- counting sort (计数排序) algorithm
- java计数排序(counting sort)
- 算法导论 第八章计数排序(counting sort)
- 计数排序(CountingSort)
- 算法导论 第八章计数排序(counting sort)
- 排序和查找-计数排序(Counting Sort)
- 算法总结系列之三 : 计数排序(CountingSort)
- counting sort 计数排序