基本排序算法--基数排序(参考桶排序,此处只复制代码)
2018-02-20 18:33
211 查看
package poly;
import java.util.Arrays;
//基数排序
public class RadixSort {
public static void main(String[] args) {
int[] array = {1100, 192, 221, 12, 13};
RadixSort t = new RadixSort();
t.sort(array, 10, 4);
System.out.println(Arrays.toString(array));
}
/**
* @方法功能说明: 基数排序
* @创建:2018年2月20日 by 北岳
* @param array : 待排序数组
* @param radix : 基数
* @param d : 最高位数
* @return void
* @throws
*/
public void sort(int[] array, int radix, int d) {
//数组长度
int length = array.length;
//存放临时数据
int[] data = new int[length];
//桶数据
int[] temp = new int[10];
for (int i = 0, rate = 1; i < d; i++) {
//保证桶干净
Arrays.fill(temp, 0);
for (int j = 0; j < length; j++) {
int key = (array[j] / rate) % radix;
temp[key] ++;
}
for (int j = 1; j < temp.length; j++) {
temp[j] = temp[j] + temp[j - 1];
}
System.arraycopy(array, 0, data, 0, length);
for (int m = length - 1; m >= 0; m--) {
int key = (data[m] / rate) % radix;
array[--temp[key]] = data[m];
}
System.out.println("第" + rate + "位排序 : " + Arrays.toString(array));
rate *= radix;
}
}
}
import java.util.Arrays;
//基数排序
public class RadixSort {
public static void main(String[] args) {
int[] array = {1100, 192, 221, 12, 13};
RadixSort t = new RadixSort();
t.sort(array, 10, 4);
System.out.println(Arrays.toString(array));
}
/**
* @方法功能说明: 基数排序
* @创建:2018年2月20日 by 北岳
* @param array : 待排序数组
* @param radix : 基数
* @param d : 最高位数
* @return void
* @throws
*/
public void sort(int[] array, int radix, int d) {
//数组长度
int length = array.length;
//存放临时数据
int[] data = new int[length];
//桶数据
int[] temp = new int[10];
for (int i = 0, rate = 1; i < d; i++) {
//保证桶干净
Arrays.fill(temp, 0);
for (int j = 0; j < length; j++) {
int key = (array[j] / rate) % radix;
temp[key] ++;
}
for (int j = 1; j < temp.length; j++) {
temp[j] = temp[j] + temp[j - 1];
}
System.arraycopy(array, 0, data, 0, length);
for (int m = length - 1; m >= 0; m--) {
int key = (data[m] / rate) % radix;
array[--temp[key]] = data[m];
}
System.out.println("第" + rate + "位排序 : " + Arrays.toString(array));
rate *= radix;
}
}
}
相关文章推荐
- 笔试面试最常涉及到的12种排序算法(包括插入排序、二分插入排序、希尔排序、选择排序、冒泡排序、鸡尾酒排序、快速排序、堆排序、归并排序、桶排序、计数排序和基数排序)进行了详解。每一种算法都有基本介绍、算
- C#.NET 剪切板复制粘贴泛型的例子代码参考 Clipboard Copy Paste List
- 桶排序与基数排序代码(JAVA)
- 三种线性的非基于比较的排序算法:计数排序、桶排序与基数排序
- 总结八大排序算法的基本思想与代码实现
- 排序算法的基本思想和OC代码实现
- 基本排序算法代码
- 六种基本排序算法思想及C代码
- 复制文章时自动在文章末尾加上来源网址的代码本文来自: http://www.cnblogs.com/wifi 详细来源请参考:http://www.cnblogs.com/wifi/archive/2012/04/24/2468097.html
- 三种线性的非基于比较的排序算法:计数排序、桶排序与基数排序
- 排序算法之计数排序、基数排序和桶排序
- [PHP实例] PHP 四种基本排序算法的代码实现
- PHP 四种基本排序算法的代码实现
- 8种基本排序算法总结及相关代码
- C# j基本操作-拷贝文件夹的所有内容到另一个文件夹内: 复制代码 1 public static void CopyDir(string srcPath, string实现文件夹的复制以及删除
- PHP 四种基本排序算法的代码实现
- PHP 四种基本排序算法的代码实现
- 参考练习,代码基本已经理解 最后取模部分有待理解。
- 【继续思考】排序算法——基数排序与桶排序
- 【参考代码】mz线段树基本操作训练