算法-基数排序
package com.jiucaiyuan.net.algrithm.sort;
import java.net.InetSocketAddress;
/**
* <pre>
* 基数排序
* 根据数据特点进行排序,而非基于比较
* 时间复杂度O(N*logN) 空间复杂度O(1)
* </pre>
* Created by jiucaiyuan on 2022/4/1.
*/
public class RadixSort {
public static void radixSort(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
radixSort(arr, 0, arr.length - 1, maxBits(arr));
}
/**
* 找出数组中最大数字的宽度
*
* @param arr
* @return
*/
public static int maxBits(int[] arr) {
int max = Integer.MIN_VALUE;
//找到最大的数
for (int i : arr) {
max = Math.max(i, max);
}
//计算宽度
int result = 0;
while (max != 0) {
result++;
max /= 10;
}
return result;
}
/**
* 针对arr[l~r]进行排序
*
* @param arr 待排序数组
* @param l 左侧下标
* @param r 右侧下标
* @param digit l到r之间最大数字的字符数(宽度)
*/
public static void radixSort(int[] arr, int l, int r, int digit) {
final int radix = 10;
int i = 0, j = 0;
//有多少个数据,就准备多少个辅助空间
int[] bucket = new int[r - l + 1];
//有多少位,就进出几次桶
for (int d = 1; d <= digit; d++) {
//10个空间
//count[0],当前位置(d位)是0的数字有多少个
//count[1],当前位置(d位)是0和1的数字有多少个
//count[2],当前位置(d位)是0、1和2的数字有多少个
//count[3],当前位置(d位)是0、1、2和3的数字有多少个
// ...
//count[i],当前位置(d位)是0~i的数字有多少个
//count[0~9]
int[] count = new int[radix];
//累计d位置出现j的次数
for (i = l; i <= r; i++) {
j = getDigit(arr[i], d);
count[j]++;
}
//count数组转化为count[i]为所有<=i的数字之和,即小于等于i的个数之和
//把count处理成前缀和
for (i = 1; i < radix; i++) {
count[i] = count[i - 1] + count[i];
}
//把i位置上的数,放到d位置上的数字及之前出现的次数-1的辅助空间中
//等于操作了入桶和出桶操作
for (i = r; i >= l; i--) {
j = getDigit(arr[i], d);
bucket[--count[j]] = arr[i];
}
//局部处理完的数组拷贝回原数组
for (i = l, j = 0; i <= r; i++, j++) {
arr[i] = bucket[j];
}
print(bucket);
}
}
/**
* 返回数字num倒数第d位置的数字
* @param num
* @param d
* @return
*/
private static int getDigit(int num, int d) {
return (num / ((int) Math.pow(10, d - 1))) % 10;
}
public static void main(String[] args) {
int[] arr = {12, 15, 63, 102, 32, 53, 908};
radixSort(arr);
System.out.println("-------------");
print(arr);
}
private static void print(int[] arr) {
if (arr == null) {
return;
}
for (int a : arr) {
System.out.print(a + "\t");
}
System.out.println();
}
}
日历
个人资料
diaba 寻求合作请留言或联系mail: services@jiucaiyuan.net
链接
最新文章
存档
- 2025年4月(17)
- 2025年3月(25)
- 2025年2月(20)
- 2025年1月(2)
- 2024年10月(1)
- 2024年8月(2)
- 2024年6月(4)
- 2024年5月(1)
- 2023年7月(1)
- 2022年10月(1)
- 2022年8月(1)
- 2022年6月(11)
- 2022年5月(6)
- 2022年4月(33)
- 2022年3月(26)
- 2021年3月(1)
- 2020年9月(2)
- 2018年8月(1)
- 2018年3月(1)
- 2017年3月(3)
- 2017年2月(6)
- 2016年12月(3)
- 2016年11月(2)
- 2016年10月(1)
- 2016年9月(3)
- 2016年8月(4)
- 2016年7月(3)
- 2016年6月(4)
- 2016年5月(7)
- 2016年4月(9)
- 2016年3月(4)
- 2016年2月(5)
- 2016年1月(17)
- 2015年12月(15)
- 2015年11月(11)
- 2015年10月(6)
- 2015年9月(11)
- 2015年8月(8)
分类
热门文章
- SpringMVC:Null ModelAndView returned to DispatcherServlet with name 'applicationContext': assuming HandlerAdapter completed request handling
- Mac-删除卸载GlobalProtect
- java.lang.SecurityException: JCE cannot authenticate the provider BC
- MyBatis-Improper inline parameter map format. Should be: #{propName,attr1=val1,attr2=val2}
- Idea之支持lombok编译
标签
最新评论
- logisqykyk
Javassist分析、编辑和创建jav... - xxedgtb
Redis—常见参数配置 - 韭菜园 ... - wdgpjxydo
SpringMVC:Null Model... - rllzzwocp
Mysql存储引擎MyISAM和Inno... - dpkgmbfjh
SpringMVC:Null Model... - tzklbzpj
SpringMVC:Null Model... - bqwrhszmo
MyBatis-Improper inl... - 乐谱吧
good非常好 - diaba
@diaba:应该说是“时间的度量依据”... - diaba
如果速度增加接近光速、等于光速、甚至大于...
最新微语
- 在每件事情上花费的东西,就是生命的一部分,而我们花费的这些东西要求立即得到回报,或者在一个长时间以后得到回报。
2025-01-23 15:46
- 诺曼·文森特说:“并不是你认为自己是什么样的人,你就是什么样的人。但是你的思想是什么样,你就是什么样的人。”
2025-01-23 15:44
- 从今天起,做一个幸福的人。喂马,砍柴,(思想)周游世界
2022-03-21 23:31
- 2022.03.02 23:37:59
2022-03-02 23:38
- 几近崩溃后,找到解决方法,总是那么豁然开朗!所以遇到问题要坚持!
2018-07-18 10:49

发表评论: