当前位置: 首页 > news >正文

【java实现+4种变体完整例子】排序算法中【基数排序】的详细解析,包含基础实现、常见变体的完整代码示例,以及各变体的对比表格

基数排序详解及代码示例

在这里插入图片描述


基数排序原理

基数排序通过处理每一位数字进行排序,分为 LSD(最低位优先)MSD(最高位优先) 两种方式。核心步骤:

  1. 确定最大值:计算数组中最大数的位数。
  2. 逐位排序:对每一位数字使用稳定排序(如计数排序)。

1. 标准LSD基数排序(处理正整数)

代码示例
public class RadixSort {public static void radixSort(int[] arr) {if (arr == null || arr.length == 0) return;int max = Arrays.stream(arr).max().getAsInt();for (int exp = 1; max / exp > 0; exp *= 10) {countSort(arr, exp);}}private static void countSort(int[] arr, int exp) {int[] output = new int[arr.length];int[] count = new int[10]; // 0-9Arrays.fill(count, 0);// 统计当前位数字的出现次数for (int value : arr) {count[(value / exp) % 10]++;}// 累加计数for (int i = 1; i < 10; i++) {count[i] += count[i - 1];}// 反向填充输出数组(保证稳定性)for (int i = arr.length - 1; i >= 0; i--) {int index = (arr[i] / exp) % 10;output[count[index] - 1] = arr[i];count[index]--;}// 替换原数组System.arraycopy(output, 0, arr, 0, arr.length);}public static void main(String[] args) {int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};radixSort(arr);System.out.println(Arrays.toString(arr)); // [2, 24, 45, 66, 75, 90, 170, 802]}
}

2. 处理负数的LSD变体

代码示例

通过偏移将负数转换为正数后再排序:

public static void radixSortWithNegative(int[] arr) {if (arr == null || arr.length == 0) return;int min = Arrays.stream(arr).min().getAsInt();if (min < 0) {// 将所有数偏移到非负区间for (int i = 0; i < arr.length; i++) {arr[i] += -min;}}int max = Arrays.stream(arr).max().getAsInt();for (int exp = 1; max / exp > 0; exp *= 10) {countSort(arr, exp);}// 恢复原始值if (min < 0) {for (int i = 0; i < arr.length; i++) {arr[i] += min;}}
}

3. 基数为16的基数排序(十六进制)

代码示例
public static void radixSortBase16(int[] arr) {int max = Arrays.stream(arr).max().getAsInt();for (int exp = 1; max / exp > 0; exp *= 16) {countSortBase16(arr, exp);}
}private static void countSortBase16(int[] arr, int exp) {int[] output = new int[arr.length];int[] count = new int[16]; // 0-15Arrays.fill(count, 0);for (int value : arr) {int digit = (value / exp) % 16;count[digit]++;}for (int i = 1; i < 16; i++) {count[i] += count[i - 1];}for (int i = arr.length - 1; i >= 0; i--) {int digit = (arr[i] / exp) % 16;output[count[digit] - 1] = arr[i];count[digit]--;}System.arraycopy(output, 0, arr, 0, arr.length);
}

4. MSD基数排序(递归实现)

代码示例
public static void msdRadixSort(int[] arr) {msdSort(arr, 0, arr.length - 1, 1); // 从最低位开始(假设初始位权为1)
}private static void msdSort(int[] arr, int low, int high, int exp) {if (low >= high) return;// 使用计数排序处理当前位int[] count = new int[10];for (int i = low; i <= high; i++) {count[(arr[i] / exp) % 10]++;}// 累加计数并移动元素for (int i = 1; i < 10; i++) {count[i] += count[i - 1];}int[] temp = new int[arr.length];for (int i = high; i >= low; i--) {int digit = (arr[i] / exp) % 10;temp[count[digit] - 1] = arr[i];count[digit]--;}// 回填到原数组for (int i = low; i <= high; i++) {arr[i] = temp[i];}// 递归处理高位for (int i = 0; i < 10; i++) {if (count[i] > 0) {msdSort(arr, low, low + count[i] - 1, exp * 10);low += count[i];}}
}

变体对比表格

变体名称差异描述时间复杂度空间复杂度稳定性
标准LSD处理正整数,从最低位到最高位排序O(nk)O(n + k)稳定
负数LSD变体处理负数,通过偏移转换为正数O(nk)O(n + k)稳定
基数为16的变体每位基数为16,适用于十六进制O(nk)O(n + 16)稳定
MSD基数排序从最高位开始,递归处理各桶O(nk)O(n + k)稳定

关键说明

  • 时间复杂度O(nk),其中 n 是元素数量,k 是位数。
  • 空间复杂度:通常为 O(n + k),因需要额外的计数数组和临时数组。
  • 稳定性:所有变体均使用计数排序作为中间步骤,因此稳定性保持。
http://www.xdnf.cn/news/29719.html

相关文章:

  • Python----深度学习(全连接与链式求导法则)
  • Java中常见的锁synchronized、ReentrantLock、ReentrantReadWriteLock、StampedLock
  • MainActivity与RecActivity之间的双向数据传递详解
  • 从 0~1 保姆级 详细版 PostgreSQL 数据库安装教程
  • 数据库备份-docker配置主从数据库
  • k8s安装kubeadm
  • 探索大语言模型(LLM):Transformer 与 BERT从原理到实践
  • 回溯算法(2):全排列问题
  • 基于DeepSeek与Excel的动态图表构建:技术融合与实践应用
  • WebSocket介绍
  • 二级评论列表-Java实现
  • 从零搭建微服务项目Pro(第6-2章——微服务鉴权模块SpringSecurity+JWT)
  • OCR技术与视觉模型技术的区别、应用及展望
  • Python语法系列博客 · 第7期[特殊字符] 列表推导式与字典推导式:更优雅地处理数据结构
  • 使用Redis实现实时排行榜
  • 【Easylive】​​Gateway模块 bootstrap.yml 解析
  • 点云数据处理开源C++方案
  • elementUI中MessageBox.confirm()默认不聚焦问题处理
  • Qt UDP 通信的详细实现步骤和示例代码
  • spring boot应用部署IIS
  • matlab论文图一的地形区域图的球形展示Version_1
  • 基于springboot的老年医疗保健系统
  • 【Matlab】中国东海阴影立体感地图
  • 【蓝桥杯 2025 省 A 扫地机器人】题解
  • Graham Scan算法求解二维凸包
  • 通过Xshell上传文件到Linux
  • Python:使用web框架Flask搭建网站
  • JS案例-Promise/A+ 规范的手写实现
  • 【厦门大学】DeepSeek大模型赋能政府数字化转型
  • OSPF实验