当前位置:网站首页>[sort] bucket sort
[sort] bucket sort
2022-07-03 13:20:00 【Programming cheese】
brief introduction
The time complexity of bucket sorting is only O(n) A sort method of , It assumes that the input data is uniformly distributed , We put the data into n A ladle , First sort the data in the bucket , Then traverse the bucket and take out the elements in the bucket in turn to complete the sorting .
Schematic diagram

For example, input data :21,8,6,11,36,50,27,42,0,12.
Then put them into the corresponding bucket for sorting , Finally, the sorting can be completed by traversing the bucket in turn and taking out the elements .
analysis
A very important step in bucket sorting is the setting of buckets , We have to depend on the input elements , Choose an appropriate “getBucketIndex” Algorithm , So that the input elements can be correctly put into the corresponding bucket , And ensure that the input data can be put into different barrels as evenly as possible .
In the worst case , That is, all the data is put into a bucket , The self sorting algorithm in bucket is insertion sorting , Then the time complexity is O(n ^ 2) 了 .
secondly , We can find out , The finer the interval is divided , The more barrels there are , In theory, the less elements are divided into each bucket , The easier it is to sort the data in a bucket , The closer the time complexity is to linear .
In extreme cases , It's just that the range is so small that it's only 1, That is, only one element is stored in the barrel , The elements in the bucket no longer need to be sorted , Because they're all the same elements , Now bucket sort is almost the same as counting sort .
Example
Assume that the input elements are evenly distributed floating-point numbers . Counting sort is more suitable for integer cases , If we still want to sort elements in linear time , Then bucket sorting will be a good choice .
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class Main {
public static void main(String[] args) {
// Input elements are in [0, 10) In this range
float[] arr = new float[] {
0.12f, 2.2f, 8.8f, 7.6f, 7.2f, 6.3f, 9.0f, 1.6f, 5.6f, 2.4f };
bucketSort(arr);
printArray(arr);
}
public static void bucketSort(float[] arr) {
// Create a new collection of buckets
ArrayList<LinkedList<Float>> buckets = new ArrayList<LinkedList<Float>>();
for (int i = 0; i < 10; i++) {
// Create a new bucket , And add it to the collection of buckets .
// Because the elements in the barrel will be inserted frequently , So choose LinkedList As the data structure of the bucket
buckets.add(new LinkedList<Float>());
}
// Put all the input data into the bucket and complete the sorting
for (float data : arr) {
int index = getBucketIndex(data);
insertSort(buckets.get(index), data);
}
// Take out all the elements in the bucket and put them in arr Medium output
int index = 0;
for (LinkedList<Float> bucket : buckets) {
for (Float data : bucket) {
arr[index++] = data;
}
}
}
/** * Calculate which bucket the input element should be placed in */
public static int getBucketIndex(float data) {
// The example here is relatively simple , Only the integer part of a floating-point number is used as the index value of its bucket
// In the actual development, it needs to be designed according to the scene
return (int) data;
}
/** * We choose insert sort as the method to sort the elements in the bucket Whenever a new element comes , We all call this method to insert it in the right place */
public static void insertSort(List<Float> bucket, float data) {
ListIterator<Float> it = bucket.listIterator();
boolean insertFlag = true;
while (it.hasNext()) {
if (data <= it.next()) {
it.previous(); // Shift the position of the iterator back to the previous position
it.add(data); // Insert data into the current position of the iterator
insertFlag = false;
break;
}
}
if (insertFlag) {
bucket.add(data); // Otherwise, insert the data into the end of the list
}
}
public static void printArray(float[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ", ");
}
System.out.println();
}
}
Link to the original text :https://blog.csdn.net/afei__/article/details/82965834
边栏推荐
- SQL learning notes (I)
- The 35 required questions in MySQL interview are illustrated, which is too easy to understand
- 8皇后问题
- OpenHarmony应用开发之ETS开发方式中的Image组件
- php:&nbsp; The document cannot be displayed in Chinese
- Some thoughts on business
- 人身变声器的原理
- Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
- 【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
- Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)
猜你喜欢

AI 考高数得分 81,网友:AI 模型也免不了“内卷”!

剑指 Offer 12. 矩阵中的路径
![[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter IV exercises]](/img/8b/bef94d11ac22e3762a570dab3a96fa.jpg)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter IV exercises]

(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)

2022-02-11 heap sorting and recursion
![[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]](/img/c0/92e9e52f1f643b66720697523a1794.png)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]

Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)

File uploading and email sending

Annotation and reflection
[email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g"/>Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
随机推荐
关于CPU缓冲行的理解
Sitescms v3.0.2 release, upgrade jfinal and other dependencies
Cadre de logback
The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
Annotation and reflection
sitesCMS v3.1.0发布,上线微信小程序
CVPR 2022 image restoration paper
2022-01-27 redis cluster technology research
2022-01-27 research on the minimum number of redis partitions
Sword finger offer 15 Number of 1 in binary
Servlet
Understanding of CPU buffer line
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
Setting up Oracle datagurd environment
道路建设问题
有限状态机FSM
剑指 Offer 17. 打印从1到最大的n位数
剑指 Offer 15. 二进制中1的个数
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]