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

做网站时间美食网页设计的制作过程

做网站时间,美食网页设计的制作过程,南京工程网站建设,淘宝搜索关键词查询工具基数排序详解及代码示例 基数排序原理 基数排序通过处理每一位数字进行排序,分为 LSD(最低位优先) 和 MSD(最高位优先) 两种方式。核心步骤: 确定最大值:计算数组中最大数的位数。逐位排序&am…

基数排序详解及代码示例

在这里插入图片描述


基数排序原理

基数排序通过处理每一位数字进行排序,分为 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.dtcms.com/wzjs/823293.html

相关文章:

  • 中企动力合作网站网络推广的优化方法
  • 在兔展上怎么做网站页面wordpress 网上商城
  • 南宁 网站建设 制作怎样做网络推广佳选豪升网络好
  • 网站搭建后台渠道网络公司官网
  • 没有网站域名是否需要备案公司起名字大全免费评分
  • 太原网站公司网站建设需要哪些证
  • 安顺网站开发苏宁易购电子商务网站建设目标
  • wordpress 点点主题seo公司推广
  • 站长统计入口wordpress 检测插件
  • 电脑十大免费游戏网站做电商网站报价
  • 南京seo整站优化技术html5网页模板免费下载
  • 网站网络优化服务器苏州手机app开发公司
  • 站酷网址是什么godaddy域名注册
  • 哪家公司做网站响水做网站哪家好
  • 网站功能建设规划书北京朝阳区天气预报
  • 一个企业网站如何能放到互联网上 vpswordpress 梦月酱
  • 怎么把网站源码上传到空间根目录视频如何给公司做自己的网站
  • 网站建设电话销售开场白微信h5商城网站开发
  • 百度网站推广费用多少dedecms小说网站模板
  • 网站搭建后显示建设中快云助手网站建设视频
  • 电子商务网站建设的方法手机作图软件app
  • 重视机关网站建设温州服务专业的网页设计
  • 成都网站建设众成联邦天眼查公司注册信息
  • 镇江建网站seo的中文含义是什么
  • 有什么网站可以做宣传镇江网
  • 建设网站需要哪些设备市总工会网站建设经验材料
  • wordpress评论钩子wordpress 优化
  • 网站开发的行业情况分析遂宁门户网站建设先进工作单位
  • 网站后台上传文章格式个体工商户是否能够做网站
  • 泉州微信网站建设公司网址导航哪个主页最好