Radix_Sort
public class Radix_sort {
public static void sort(int[] arrays,int radix){
int n = 1;
int length = arrays.length;
int[][] bucket = new int[10][length]; //保存根据元素某个位数上的数字的分组结果
int[] count=new int[length]; //用来存储每个数字的个数
int flag = 0; //判断排序是否结束
Arrays.fill(count, 0); //初始化count为0
while(flag < length){
flag = 0 ;//每次排序前置0
for(int i = 0; i < length; i++){
int index = (arrays[i] / n)% radix; //取该元素n位数上的数字
bucket[index][count[index]++] = arrays[i]; //将该元素保存到对应的数组
if(arrays[i] / n == 0){ //如果元素除以n 为0 代表该元素的每位数已经排完序
flag ++;
}
}
int num = 0;
for(int i = 0; i < 10; i++){
for(int j = 0; j < count[i]; j++){ //将分组结果重新保存到原数组中 新的数组已经按照每个元素的 某位数进行了排序
arrays[num++] = bucket[i][j];
}
count[i] = 0; //将每个分组的统计个数置为0
}
n *= 10; //位数向前移一位
}
}
public static void main(String[] args){
int[] A=new int[]{300,222, 8875, 60, 104110, 14, 5555, 30, 77754, 45, 105131};
sort(A, 10);
for(int num:A)
{
System.out.println(num);
}
}
}
Radix_Sort的更多相关文章
- CUDA程序设计(三)
算法设计:基数排序 CUDA程序里应当尽量避免递归,因而在迭代排序算法里,基数排序通常作为首选. 1.1 串行算法实现 十进制位的基数排序需要考虑数位对齐问题,比较麻烦.通常实现的是二进制位的基数排序 ...
- 八大排序算法的 Python 实现
转载: 八大排序算法的 Python 实现 本文用Python实现了插入排序.希尔排序.冒泡排序.快速排序.直接选择排序.堆排序.归并排序.基数排序. 1.插入排序 描述 插入排序的基本操作就是将一个 ...
- 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较
2.3 归并排序 接口定义: int merge(void* data, int esize, int lpos, int dpos, int rpos, int (*compare)(const v ...
- Codeforces 427 D. Match & Catch
后缀数组.... 在两个串中唯一出现的最小公共子串 D. Match & Catch time limit per test 1 second memory limit per test 51 ...
- C语言中的七种排序算法
堆排序: void HeapAdjust(int *arraydata,int rootnode,int len) { int j; int t; *rootnode+<len) { j=*ro ...
- 各种排序算法代码(C语言版)
选择排序 #include <stdio.h> /* * 选择排序 * 稳定性:不稳定 * 时间复杂度:O(N^2) **/ void select_sort(int a[], int l ...
- 桶排序与基数排序代码(JAVA)
桶排序 publicstaticvoid bucketSort(int[] a,int max){ int[] buckets; if(a==null || m ...
- 你需要知道的九大排序算法【Python实现】源码
#coding: utf-8 #!/usr/bin/python import randomimport math #随机生成0~100之间的数值def get_andomNumber(num): l ...
- 你需要知道的九大排序算法【Python实现】之基数排序
八.基数排序 基本思想:基数排序(radix sort)属于"分配式排序"(distribution sort),又称"桶子法"(bucket sort)或bi ...
随机推荐
- go 错误处理与测试
Go 没有像 Java 和 .NET 那样的 try/catch 异常机制:不能执行抛异常操作.但是有一套 defer-panic-and-recover 机制(参见 13.2-13.3 节). Go ...
- "Flex弹性布局"组件:<flex-row><flex-col> —— 快应用组件库H-UI
 <import name="flex-row" src="../Common/ui/h-ui/basic/c_flex_row"></im ...
- matplotlib formatters
Tick formatting is controlled by classes derived from Formatter. The formatteroperates on a single ...
- 更快地访问stackoverflow
使用火狐浏览器,安装扩展组件 Decentraleyes, 完成 原理:由于爆栈本身并没有被墙, 但使用了google的api,而google的api是被墙的. 该组件替换了国内不能访问的api,所以 ...
- Python设计模式(8)-抽象工厂
# coding=utf-8 这种方式反倒把事情做复杂了 可取之处在于有了更高层次的抽象 class IEmployee: def insert_employee(self): pass class ...
- java 的 数字、汉字 和 字母 的所占字节长度 与 字符长度 (邮件限制50个汉字)
public static void main(String[] args) { String a = "餿餿餿餿餿z"; byte[] bytes = a.getBytes( ...
- python填写问卷星,疫情上报
#!!!注意:修改main里的url为真实的url,按需修改 50行 set_data中的submitdata # 61行 ip 修改为 真ip # submittype可能有错误,在151行 # 提 ...
- CTE(With As)
WITH tabdate(dt) AS ( FROM dual UNION ALL FROM tabdate WHERE dt ) SELECT * FROM TabDate ; 一.With Tab ...
- poi导出word文档,doc和docx
maven <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency> <gro ...
- 12. 前后端联调 + ( proxy代理 ) + ( axios拦截器 ) + ( css Modules模块化方案 ) + ( css-loader ) + ( 非路由组件如何使用history ) + ( bodyParser,cookieParser中间件 ) + ( utility MD5加密库 ) + ( nodemon自动重启node ) + +
(1) proxy 前端的端口在:localhost:3000后端的端口在:localhost:1234所以要在webpack中配置proxy选项 (proxy是代理的意思) 在package.jso ...