计数排序
计数是一种适合元素均为大于等于零的整数,且最大值与最小值差值不大的排序
将数组元素作为数组下标,用一个临时数组统计每个元素出现的个数,再将临时数组从小到大输出,就得到了排序好的数组
比如 2,5,8,9,6,6,1这几个数排序,令临时数组长度为 10,当读入2时,count[2]++,所有数据读完后的count数组如下
count[0] = 0
count[1] = 1
count[2] = 1
count[3] = 0
count[4] = 0
count[5] = 1
count[6] = 2
count[7] = 0
count[8] = 1
count[9] = 1
输出这个数组就得到了排序后的数据:1,2,5,6,6,8,9
上面的思想中实质上临时数组的大小M要大于最大元素,但在一些情况下如数组为[1001,1002,1007]时,再使用上面的算法就会发现存在大量的无用空间,因此我们可以使临时数组大小为M = (Max - Min + 1),用Min作为偏移量
public static void countingsort(int[] a)
{
//寻找最大值
int max = 0;
for(int i = 0; i < a.length; i++)
if(max < a[i])
max = a[i];
//创建临时数组
int[] count = new int[max + 1];
//统计元素
for(int i = 0; i < a.length; i++)
count[a[i]]++;
//输出临时数组所含信息
int j = 0;
for(int i = 0; i < count.length; i++)
for(; count[i] != 0; count[i]--)
a[j++] = i;
}
优化后
public static void countingsort(int[] a)
{
//寻找最大值与最小值
int max = 0;
int min = 0;
for(int i = 0; i < a.length; i++)
{
if(max < a[i])
max = a[i];
if(min > a[i])
min = a[i];
}
//创建优化后的临时数组
int[] count = new int[max - min + 1];
//统计元素
for(int i = 0; i < a.length; i++)
count[a[i] - min]++;
//输出临时数组所含信息
int j = 0;
for(int i = 0; i < count.length; i++)
for(; count[i] != 0; count[i]--)
a[j++] = i + min;
}
算法分析
- 最好、最坏、平均时间复杂度为O(N + k),k 是临时数组的长度
- 空间复杂度为O(k)
- 是稳定的算法
桶排序
代码
/**
* 桶排序步骤
* 1.找出最大最小值,确定桶的个数 2.初始化桶 3.对通道内部排序4.进行输出
*/
public class bucketSort {
public static void sort_tong(int[] nums){
//寻找数组中的最大最小值
int max = nums[0];
int min = nums[0];
for (int i =0 ;i < nums.length;i++){
if (max < nums[i])
max = nums[i];
if (min > nums[i])
min = nums[i];
}
//初始化桶
int bucketNum = (max-min)/nums.length+1;
ArrayList<LinkedList<Integer>> bucket = new ArrayList<>(bucketNum);
for (int i=0;i<bucketNum;i++){
bucket.add(new LinkedList<Integer>());
}
//将元素放入对应的桶中
for (int i =0;i<nums.length;i++){
int index = (nums[i]-min)/nums.length;
bucket.get(index).add(nums[i]);
}
//对每个桶内部的元素排序
for (int i =0 ;i<bucket.size();i++){
Collections.sort(bucket.get(i));
}
//将元素输出
int k = 0;
for (LinkedList<Integer> in : bucket){
for (Integer i : in)
nums[k++] = i;
}
}
public static void main(String[] args) {
int[] nums={5,4,45,2,5,8};
sort_tong(nums);
for (int i =0;i<nums.length;i++){
System.out.printf("%d ",nums[i]);
}
}
算法分析
- 算法的平均、最好时间复杂度为O(n + k),最坏 时间复杂度为O(n2),k为桶的个数
- 空间复杂度为O(n + k)
- 需要额外空间/辅助数组
- 稳定性取决于内部排序方式
基数排序
基数排序是一种按位进行的桶排序,这样可以减少桶的数量。基数排序的第一步是将所有数值补为同样的位数长度,不够的前面补零
最高位优先:首先按照最高位对元素进行桶排序,一个桶里可能有多个元素,再将这个桶中的多个元素按次高位排序,递归地进行桶排序,直到每个元素都存在一个桶中,遍历就的到了排序后的数组
最高位优先尽管使用了比直桶排序更少数量的桶,但仍然过于麻烦,而且需要使用递归,大桶里有小桶,因此一般使用最低为优先
最低位优先:首先按照最低位进行桶排序,一个桶中的多个元素将一趟排序后的数组再进行次低位的桶排序,以此类推直到排完最高位,那么遍历数组就得到了排序好的元素
代码
/**
* 基数排序,
*/
public class RadixSort {
public static void radixSort(int[] nums){
//寻找最大值
int max=nums[0];
for (int i =1;i<nums.length;i++){
if (max<nums[i]){
max = nums[i];
}
}
System.out.println(String.valueOf(max));
//计算最高位有几位
int maxFigure = 1;
while(max/10 > 0){
maxFigure++;
max/=10;
}
System.out.println(String.valueOf(maxFigure));
//初始化桶
ArrayList<LinkedList<Integer>> bucket = new ArrayList<>(10);
for (int i=0;i<10;i++){
bucket.add(new LinkedList<Integer>());
}
//进行每一趟的排序,最低位优先
for (int i =1;i<=maxFigure;i++){
//获取每一位的最后一位的值
for (int num : nums) {
//pow(x,y)求x的y次方
int val = (num / (int) Math.pow(10, i - 1)) % 10;
bucket.get(val).add(num);
}
//每一趟排序后将元素放入元素组
int k=0;
for (int j=0;j<10;j++){
for (Integer x:bucket.get(j))
nums[k++]= x;
//一趟排序以后将桶清空
bucket.get(j).clear();
}
}
}
public static void main(String[] args) {
int[] nums = {9999,1};
radixSort(nums);
for (int i = 0;i<nums.length;i++){
System.out.printf("%d ",nums[i]);
}
//System.out.println(nums.toString());
}
算法分析
- 最低位优先使用的桶数量远小于最高位优先
- 时间复杂度为O(N*k),k是桶的个数
- 空间复杂度为O(N + k)
- 需要辅助数组
- 是稳定的排序