博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java AmericanFlagSort
阅读量:4947 次
发布时间:2019-06-11

本文共 3226 字,大约阅读时间需要 10 分钟。

Java AmericanFlagSort

/** *  *  *  

Copyright 1994-2018 JasonInternational

*

All rights reserved.

*

Created on 2018年4月10日

*

Created by Jason

* * */package cn.ucaner.algorithm.sorts;/** * An American flag sort is an efficient, in-place variant of radix sort that * distributes items into hundreds of buckets. Non-comparative sorting * algorithms such as radix sort and American flag sort are typically used to * sort large objects such as strings, for which comparison is not a unit-time * operation. *

* Family: Bucket.

* Space: In-place.
* Stable: False.
*

* Average case = O(n*k/d)

* Worst case = O(n*k/d)
* Best case = O(n*k/d)
*

* NOTE: n is the number of digits and k is the average bucket size *

* @see American Flag Sort (Wikipedia) *

* @author Justin Wetherell
*/public class AmericanFlagSort { private static final int NUMBER_OF_BUCKETS = 10; // 10 for base 10 numbers private AmericanFlagSort() { } public static Integer[] sort(Integer[] unsorted) { int numberOfDigits = getMaxNumberOfDigits(unsorted); // Max number of digits int max = 1; for (int i = 0; i < numberOfDigits - 1; i++) max *= 10; sort(unsorted, 0, unsorted.length, max); return unsorted; } public static void sort(Integer[] unsorted, int start, int length, int divisor) { // First pass - find counts int[] count = new int[NUMBER_OF_BUCKETS]; int[] offset = new int[NUMBER_OF_BUCKETS]; int digit = 0; for (int i = start; i < length; i++) { int d = unsorted[i]; digit = getDigit(d, divisor); count[digit]++; } offset[0] = start + 0; for (int i = 1; i < NUMBER_OF_BUCKETS; i++) { offset[i] = count[i - 1] + offset[i - 1]; } // Second pass - move into position for (int b = 0; b < NUMBER_OF_BUCKETS; b++) { while (count[b] > 0) { int origin = offset[b]; int from = origin; int num = unsorted[from]; unsorted[from] = -1; do { digit = getDigit(num, divisor); int to = offset[digit]++; count[digit]--; int temp = unsorted[to]; unsorted[to] = num; num = temp; from = to; } while (from != origin); } } if (divisor > 1) { // Sort the buckets for (int i = 0; i < NUMBER_OF_BUCKETS; i++) { int begin = (i > 0) ? offset[i - 1] : start; int end = offset[i]; if (end - begin > 1) sort(unsorted, begin, end, divisor / 10); } } } 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; }}

  

转载于:https://www.cnblogs.com/jasonandy/p/9243193.html

你可能感兴趣的文章
2014百度之星资格赛的第二个问题
查看>>
动态方法决议 和 消息转发
查看>>
关于UI资源获取资源的好的网站
查看>>
Nginx代理访问提示ERR_CONTENT_LENGTH_MISMATCH异常的解决方案
查看>>
WPF自定义搜索框代码分享
查看>>
js 基础拓展
查看>>
Windows下常用测试命令
查看>>
SpringBoot访问html访问不了的问题
查看>>
{width=200px;height=300px;overflow:hidden}
查看>>
SDK提交到CocoaPods
查看>>
C#生成随机数
查看>>
Flask-jinja2
查看>>
【BZOJ3224】Tyvj 1728 普通平衡树 Splay
查看>>
java8 四大核心函数式接口Function、Consumer、Supplier、Predicate
查看>>
iOS 图片加载速度极限优化—FastImageCache解析
查看>>
类的声明和使用
查看>>
bzoj3196: Tyvj 1730 二逼平衡树
查看>>
CSS基础学习 20.CSS媒体查询
查看>>
软件测试分类
查看>>
Eureka
查看>>