排序算法之基数排序(JAVA)
2012-12-28 11:00
253 查看
public class RadixSort { /** * 基数排序 * 简介:基数排序法又称“桶子法”,顾名思义,它是透过键值的部份资讯,将要排序的元素分配至某些“桶”中,藉以达到排序的作用 * 其时间复杂度为O (nlog(r)m),其中r为所采取的基数,而m为堆数 * 稳定排序方式 * @param nums 待排序数组 * @d 基数 */ public static void sort(int[] nums,int d){ int k = 0; int n = 1; int len = nums.length; //分成nums.length个桶 int[][] radixArray = new int[len][len]; //每个桶放的个数组成的数组 int[] tempArray = new int[len]; //还在位数内 while (n<=d) { for (int i = 0; i < len; i++) { //个,十,百,千... int temp = (nums[i]/n)%10; //存入特定桶的特定位置 radixArray[temp][tempArray[temp]] = nums[i]; tempArray[temp]++; } for (int i = 0; i < len; i++) { if (tempArray[i] != 0) { for (int j = 0; j < tempArray[i]; j++) { //数组重组 nums[k] = radixArray[i][j]; k++; } //重置,以防下次循环时数据出错 tempArray[i] = 0; } } //重置,以防下次循环时数据出错 k = 0; //进位 n *=10; } } // 基数排序的实现 public static void main(String[] args) { int[] nums = { 2, 7, 8, 3, 1, 6, 9, 0, 5, 4 }; RadixSort.sort(nums, 10); System.out.println(Arrays.toString(nums)); } }
相关文章推荐
- 【Java】八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序 、快速排序、归并排序、堆排序和LST基数排序
- Java排序算法总结(八):基数排序
- Java实现-高效排序算法之基数排序
- Java排序算法以及算法改进总结(计数排序、基数排序、桶排序)
- Java排序算法总结之(五)—— 基数排序
- 排序算法复习(Java实现)(二): 归并排序,堆排序,桶式排序,基数排序
- Java排序算法(十一):基数排序
- 排序算法复习(Java实现)(二): 归并排序,堆排序,桶式排序,基数排序
- 易解排序算法 - 空间换取时间(java写:基数排序,计数排序,桶排序,排序全部源代码)
- java必须知道的八大种排序算法:冒泡排序、 选择排序、插入排序、快速排序、希尔算法、归并排序算法、基数排序、堆排序算法
- 排序算法java 二 --基数排序、归并排序
- 排序算法java版,速度排行:冒泡排序、简单选择排序、直接插入排序、折半插入排序、希尔排序、堆排序、归并排序、快速排序
- 数据结构 学习笔记(十一):排序(下):快速 / 表 / 桶 / 基数 排序,排序算法的比较
- 排序算法之快速排序--Java语言
- 排序算法--插入排序(直接插入排序、折半插入、shell排序)的java实现
- 排序算法之直接插入排序(java)
- 排序算法 ---选择排序(直排,堆排)(java)
- Java排序算法(七):快速排序
- 几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- 排序算法(四) 基数排序