基数排序和计数排序

首先,基数排序
基数排序,算法的复杂度大概是 时间复杂度 为O (nlog(r)m),其中r为所采取的基数,而m为堆数
以下为摘抄

第一步

以LSD为例,假设原来有一串数值如下所示:
73, 22, 93, 43, 55, 14, 28, 65, 39, 81
首先根据个位数的数值,在走访数值时将它们分配至编号0到9的桶子中:
0
1 81
2 22
3 73 93 43
4 14
5 55 65
6
7
8 28
9 39

第二步

接下来将这些桶子中的数值重新串接起来,成为以下的数列:
81, 22, 73, 93, 43, 14, 55, 65, 28, 39
接着再进行一次分配,这次是根据十位数来分配:
0
1 14
2 22 28
3 39
4 43
5 55
6 65
7 73
8 81
9 93

第三步

接下来将这些桶子中的数值重新串接起来,成为以下的数列:
14, 22, 28, 39, 43, 55, 65, 73, 81, 93
这时候整个数列已经排序完毕;如果排序的对象有三位数以上,则持续进行以上的动作直至最高位数为止。

这个基数排序,分为MSD和LSD,即最低位排序和最高位排序,就是反过来倒过去,我总感觉,最高位排序的适用性要广泛一点,也更容易代码实现。



计数排序

先得解释下偏序集了...
偏序集:自反,反对称,传递的集合,叫偏序集。通俗一点说,就是一个集合S,里边任意的x,y,z满足
x>=x,x<=x
if x<=y and x>=y then x==y
if x<=y and y<=z then x<=z
很简单的定义。
java代码如下,慢喷,看意思就行
package lengyu.countingSort;

public class CountingSort {
public static int[] countingSort(int[] unsortArray){
int length = unsortArray.length;
int[] count = new int[length];
int[] sortedArray = new int[length];
for(int i = 0 ; i < length ; i ++){
for(int j = i ; j < length ; j ++ ){
if(unsortArray[i]
count[j]++;
}
else if(unsortArray[i]>unsortArray[j]){
count[i]++;
}
}
}
for(int i = 0 ; i < length ; i ++){
sortedArray[count[i]] = unsortArray[i];
}
return sortedArray;
}
public static void main(String[] args) {
int[] unsortArray = {2,6,1};
System.out.println(countingSort(unsortArray)[2]);
}

}

计数排序代码有待更新,抱歉。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值