Java RadixSort

/**
* <html>
* <body>
* <P> Copyright 1994-2018 JasonInternational </p>
* <p> All rights reserved.</p>
* <p> Created on 2018年4月10日 </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.algorithm.sorts; import java.util.Arrays; /**
* Radix sort is a non-comparative integer sorting algorithm that sorts data
* with integer keys by grouping keys by the individual digits which share the
* same significant position and value. A positional notation is required, but
* because integers can represent strings of characters (e.g., names or dates)
* and specially formatted floating point numbers, radix sort is not limited to
* integers.
* <p>
* Family: Bucket.<br>
* Space: 10 Buckets with at most n integers per bucket.<br>
* Stable: True.<br>
* <p>
* Average case = O(n*k)<br>
* Worst case = O(n*k)<br>
* Best case = O(n*k)<br>
* <p>
* NOTE: n is the number of digits and k is the average bucket size
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Radix_sort">Radix Sort (Wikipedia)</a>
* <br>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class RadixSort { private static final int NUMBER_OF_BUCKETS = 10; // 10 for base 10 numbers private RadixSort() { } public static Integer[] sort(Integer[] unsorted) {
int[][] buckets = new int[NUMBER_OF_BUCKETS][10];
for (int i = 0; i < NUMBER_OF_BUCKETS; i++)
buckets[i][0] = 1; // Size is one since the size is stored in first element
int numberOfDigits = getMaxNumberOfDigits(unsorted); // Max number of digits
int divisor = 1;
for (int n = 0; n < numberOfDigits; n++) {
int digit = 0;
for (int d : unsorted) {
digit = getDigit(d, divisor);
buckets[digit] = add(d, buckets[digit]);
}
int index = 0;
for (int i = 0; i < NUMBER_OF_BUCKETS; i++) {
int[] bucket = buckets[i];
int size = bucket[0];
for (int j = 1; j < size; j++) {
unsorted[index++] = bucket[j];
}
buckets[i][0] = 1; // reset the size
}
divisor *= 10;
}
return unsorted;
} private static int getMaxNumberOfDigits(Integer[] unsorted) {
int max = Integer.MIN_VALUE;
int temp = 0;
for (int i : unsorted) {
temp = (int) Math.log10(i) + 1;
if (temp > max)
max = temp;
}
return max;
} private static int getDigit(int integer, int divisor) {
return (integer / divisor) % 10;
} private static int[] add(int integer, int[] bucket) {
int size = bucket[0]; // size is stored in first element
int length = bucket.length;
int[] result = bucket;
if (size >= length) {
result = Arrays.copyOf(result, ((length * 3) / 2) + 1);
}
result[size] = integer;
result[0] = ++size;
return result;
}
}

  

Java RadixSort的更多相关文章

  1. Java常用的排序算法三

    Merge Sort :归并排序:用递归的思想,分解成单个元素的排序,在归并 代码: import java.util.*; public class MergeSort { public stati ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. 8个排序算法——java

    public static void radixsort(int[] a){ int max=a[0]; for(int i=1;i<a.length;i++){ if (max<a[i] ...

  4. 八大排序算法Java

    目录(?)[-] 概述 插入排序直接插入排序Straight Insertion Sort 插入排序希尔排序Shells Sort 选择排序简单选择排序Simple Selection Sort 选择 ...

  5. Java程序员必须掌握的8大排序算法

    分类: 1)插入排序(直接插入排序.希尔排序)2)交换排序(冒泡排序.快速排序)3)选择排序(直接选择排序.堆排序)4)归并排序5)分配排序(基数排序) 所需辅助空间最多:归并排序所需辅助空间最少:堆 ...

  6. 基本排序算法——基数排序java实现

    基数排序 package basic.sort; import java.util.Arrays; import java.util.Random; public class RadixSort { ...

  7. 数据结构作业之用队列实现的基数排序(Java版)

    题目: 利用队列实现对某一个数据序列的排序(采用基数排序),其中对数据序列的数据(第1和第2条进行说明)和队列的存储方式(第3条进行说明)有如下的要求: 1)当数据序列是整数类型的数据的时候,数据序列 ...

  8. 基数排序 java 实现

    基数排序 java 实现 Wikipedia: Radix sort geeksforgeeks: Radix sort 数学之美番外篇:快排为什么那样快 Java排序算法总结(八):基数排序 排序八 ...

  9. Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法

    Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排 ...

随机推荐

  1. DIOCP任务队列和工作线程

    DIOCP任务队列和工作线程 涉及4个单元文件:utils_strings.pas,utils_queues.pas,utils_queueTask.pas,utils_grouptask.pas. ...

  2. python文件导出exe可执行程序

    开门见山的说: 1.安装pyinstaller.(windows 用pip3 Mac 用pip)在cmd中输入:pip3 install pyinstaller 2.找到你要打包的文件的目录的上一个目 ...

  3. OpenTK学习笔记(2)-工作窗口的三种方法创建方法(winfrom窗体控件形式创建)

    参考资料: https://social.msdn.microsoft.com/Forums/zh-TW/1b781685-c670-4338-953d-1957a8f24a66/opentkglco ...

  4. 使用Expression动态创建lambda表达式

    using System;using System.Linq.Expressions;using System.Reflection; namespace Helper{ public class L ...

  5. presto计算日期间隔天数或者小时间隔——date_diff函数使用

    “Presto是Facebook最新研发的数据查询引擎,可对250PB以上的数据进行快速地交互式分析.据称该引擎的性能是 Hive 的 10 倍以上.”,亲身用过之后,觉得比hive快了10倍不止. ...

  6. LC 486. Predict the Winner

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  7. SQL-W3School-函数:SQL COUNT() 函数

    ylbtech-SQL-W3School-函数:SQL COUNT() 函数 1.返回顶部 1. COUNT() 函数返回匹配指定条件的行数. SQL COUNT() 语法 SQL COUNT(col ...

  8. 123457123456#0#-----com.threeapp.XianshiDaDiShu03-----现实版打地鼠03

    com.threeapp.XianshiDaDiShu03-----现实版打地鼠03

  9. SpringCloud学习成长之路二 服务客户端(rest+ribbon)

    在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的. Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是f ...

  10. JavaScript this指向问题

    this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定,this最终指向调用它的对象. 1.函数调用模式: 当一个函数并非一个对象的属性时,那么它就是被当做函数来调用的.在此种模式下, ...