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. routine的加载

    // Hearthbuddy.Windows.MainWindow // Token: 0x06000245 RID: 581 RVA: 0x0008C318 File Offset: 0x0008A ...

  2. 【译】Solr in Action 第二章

    2.1 2.2 2.3 基本废话 2.4 基本废话

  3. LC 989. Add to Array-Form of Integer

    For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  ...

  4. 深度学习框架PyTorch一书的学习-第七章-生成对抗网络(GAN)

    参考:https://github.com/chenyuntc/pytorch-book/tree/v1.0/chapter7-GAN生成动漫头像 GAN解决了非监督学习中的著名问题:给定一批样本,训 ...

  5. C++ - 第一个程序

    代码: #include <iostream> using namespace std; int main() { cout << "hello!" < ...

  6. query解决touchmove时屏蔽touchend

    var dragging = false; $("li").on("touchmove",function(){ dragging = true; }); $( ...

  7. xcode出现the file couldn't be opened怎么解决

    右键——show In finder——显示xcode包内容——将有数字的删除——把有用的xcode双击

  8. python 优雅的解析 jsonp

    一段 jsonp 格式数据 mtopjsonpweexcb1({"api":"mtop.taobao.idle.recycle.nextspunav.get", ...

  9. vue时间戳转换(10位数)/(13位)

    <template> <!-- time为时间戳 --> <div>{{time | formatDate}}</div> <!-- 结果为 20 ...

  10. await/async闲说

    C#中await/async闲说 自从C#5.0增加异步编程之后,异步编程越来越简单,async和await用的地方越来越多,越来越好用,只要用异步的地方都是一连串的异步,如果想要异步编程的时候,需要 ...