非比较排序:计数排序、桶排序、基数排序
计数排序不是基于比较的排序算法,其核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。 作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。
Shell
public static void sortCount(int[] arr, int m, int n) { int len = arr.length; int[] tem = new intn - m + 1; for(int i = 0; i < len; i++) { tem[arri - m] += 1; } for(int i = 0, index = 0; i < tem.length; i++) { int item = temi; while(item-- != 0) { arrindex++ = i + m; } } }
12345678910111213 | public static void sortCount(int[] arr, int m, int n) { int len = arr.length; int[] tem = new intn - m + 1; for(int i = 0; i < len; i++) { tem[arri - m] += 1; } for(int i = 0, index = 0; i < tem.length; i++) { int item = temi; while(item-- != 0) { arrindex++ = i + m; } } } |
---|
计数排序是一个稳定的排序算法。当输入的元素是 n 个 0 到 k 之间的整数时,时间复杂度是 O (n+k),空间复杂度也是 O (n+k),其排序速度快于任何比较排序算法。当 k 不是很大并且序列比较集中时,计数排序是一个很有效的排序算法。
桶排序是计数排序的升级版。它利用了函数的映射关系,高效与否的关键就在于这个映射函数的确定。桶排序 (Bucket sort)的工作的原理:假设输入数据服从均匀分布,将数据分到有限数量的桶里,每个桶再分别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排)。
Shell
static void bucketSort(int[] a, int max) { int[] buckets; if (a == null || max < 1) return; // 创建一个容量为max的数组buckets,并且将buckets中的有数据都初始化为0。 buckets = new intmax + 1; // 1. 计数 for(int i = 0;i < a.length;i++) buckets[ai]++; // 2. 排序 for(int i = 0,j = 0;i <= max;i++) while((bucketsi--) > 0) aj++ = i; buckets = null; }
123456789101112131415 | static void bucketSort(int[] a, int max) { int[] buckets; if (a == null || max < 1) return; // 创建一个容量为max的数组buckets,并且将buckets中的有数据都初始化为0。 buckets = new intmax + 1; // 1. 计数 for(int i = 0;i < a.length;i++) buckets[ai]++; // 2. 排序 for(int i = 0,j = 0;i <= max;i++) while((bucketsi--) > 0) aj++ = i; buckets = null;} |
---|
桶排序最好情况下使用线性时间 O (n),桶排序的时间复杂度,取决与对各个桶之间数据进行排序的时间复杂度,因为其它部分的时间复杂度都为 O (n)。很显然,桶划分的越小,各个桶之间的数据越少,排序所用的时间也会越少。但相应的空间消耗就会增大。
基数排序是按照低位先排序,然后收集;再按照高位排序,然后再收集;依次类推,直到最高位。有时候有些属性是有优先级顺序的,先按低优先级排序,再按高优先级排序。最后的次序就是高优先级高的在前,高优先级相同的低优先级高的在前。
Shell
public void sort(int[] array) { //首先确定排序的趟数; int max = array0; for (int i = 1; i < array.length; i++) { if (arrayi > max) { max = arrayi; } } int time = 0; //判断位数; while (max > 0) { max /= 10; time++; } //建立10个队列; List<ArrayList> queue = new ArrayList<ArrayList>(); for (int i = 0; i < 10; i++) { ArrayList<Integer> queue1 = new ArrayList<Integer>(); queue.add(queue1); } //进行time次分配和收集; for (int i = 0; i < time; i++) { //分配数组元素; for (int j = 0; j < array.length; j++) { //得到数字的第time+1位数; int x = arrayj % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i); ArrayList<Integer> queue2 = queue.get(x); queue2.add(arrayj); queue.set(x, queue2); } int count = 0;//元素计数器; //收集队列元素; for (int k = 0; k < 10; k++) { while (queue.get(k).size() > 0) { ArrayList<Integer> queue3 = queue.get(k); arraycount = queue3.get(0); queue3.remove(0); count++; } } } }
123456789101112131415161718192021222324252627282930313233343536373839404142 | public void sort(int[] array) { //首先确定排序的趟数; int max = array0; for (int i = 1; i < array.length; i++) { if (arrayi > max) { max = arrayi; } } int time = 0; //判断位数; while (max > 0) { max /= 10; time++; } //建立10个队列; List<ArrayList> queue = new ArrayList<ArrayList>(); for (int i = 0; i < 10; i++) { ArrayList<Integer> queue1 = new ArrayList<Integer>(); queue.add(queue1); } //进行time次分配和收集; for (int i = 0; i < time; i++) { //分配数组元素; for (int j = 0; j < array.length; j++) { //得到数字的第time+1位数; int x = arrayj % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i); ArrayList<Integer> queue2 = queue.get(x); queue2.add(arrayj); queue.set(x, queue2); } int count = 0;//元素计数器; //收集队列元素; for (int k = 0; k < 10; k++) { while (queue.get(k).size() > 0) { ArrayList<Integer> queue3 = queue.get(k); arraycount = queue3.get(0); queue3.remove(0); count++; } } } } |
---|
基数排序基于分别排序,分别收集,所以是稳定的。但基数排序的性能比桶排序要略差,每一次关键字的桶分配都需要 O (n) 的时间复杂度,而且分配之后得到新的关键字序列又需要 O (n) 的时间复杂度。假如待排数据可以分为 d 个关键字,则基数排序的时间复杂度将是 O (d*2n) ,当然 d 要远远小于 n,因此基本上还是线性级别的。基数排序的空间复杂度为 O (n+k),其中 k 为桶的数量。一般来说 n>>k,因此额外空间需要大概 n 个左右。