Java AmericanFlagSort
Java AmericanFlagSort
/**
* <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; /**
* 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.
* <p>
* Family: Bucket.<br>
* Space: In-place.<br>
* Stable: False.<br>
* <p>
* Average case = O(n*k/d)<br>
* Worst case = O(n*k/d)<br>
* Best case = O(n*k/d)<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/American_flag_sort">American Flag Sort (Wikipedia)</a>
* <br>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
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;
}
}
Java AmericanFlagSort的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- Java多线程基础学习(二)
9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...
- Java多线程基础学习(一)
1. 创建线程 1.1 通过构造函数:public Thread(Runnable target, String name){} 或:public Thread(Runnable target ...
随机推荐
- Java 内存模型学习笔记
1.Java类 public class Math { public static final Integer CONSTANT = 666; public int math(){ int a = 1 ...
- 艺赛旗RPA谷歌浏览器拾取
rpa通过chrome拾取,操作步骤如下 方法一: 1>安装谷歌访问助手 直接下载:谷歌访问助手 官方下载地址:https://github.com/haotian-wang/google-ac ...
- 使用Flask构建机器学习模型API
1. Python环境设置和Flask基础 使用"Anaconda"创建一个虚拟环境.如果你需要在Python中创建你的工作流程,并将依赖项分离出来,或者共享环境设置," ...
- leetcode 108. Convert Sorted Array to Binary Search Tree 、109. Convert Sorted List to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 这个题使用二分查找,主要要注意边界条件. 如果left > right,就返回NULL.每次更新的 ...
- pytorch加载数据的方法-没弄,打算弄
参考:https://www.jianshu.com/p/aee6a3d72014 # 网络,netg为生成器,netd为判别器 netg, netd = NetG(opt), NetD(opt) # ...
- Tracker 服务器地址大全 Tracker List
https://dns.icoa.cn/tracker/ udp://tracker.tiny-vps.com:6969/announce https://1337.abcvg.info/announ ...
- 阿里fastjson工具类
package com.common.utils.jsonUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JS ...
- python 中的一些基础算法:递归/冒泡/选择/插入
递归算法 如果一个函数包含了对自己的调用,那么这个函数就是递归的. 比如我们计算下1-7乘法的计算: def func(n): if n ==1 : return 1 return n*func(n- ...
- c#子线程执行完怎么通知主线程(转)
定义一个委托实现回调函数 public delegate void CallBackDelegate(string message); 程序开始的时候 //把回调的方法给委托变量 CallBackDe ...
- videojs改变音量大小
<audio id=example-video preload="auto" class="video-js vjs-default-skin" type ...