当前位置:网站首页>[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
边栏推荐
- 人身变声器的原理
- Elk note 24 -- replace logstash consumption log with gohangout
- Sword finger offer 11 Rotate the minimum number of the array
- mysql更新时条件为一查询
- Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
- json序列化时案例总结
- Mysql database basic operation - regular expression
- Logback 日志框架
- The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
- Logback 日志框架
猜你喜欢
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
今日睡眠质量记录77分
Multi table query of MySQL - multi table relationship and related exercises
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
对业务的一些思考
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
Solve system has not been booted with SYSTEMd as init system (PID 1) Can‘t operate.
这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
STM32 and motor development (from MCU to architecture design)
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
随机推荐
C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
2022-01-27 redis cluster technology research
【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
关于CPU缓冲行的理解
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
Convolution emotion analysis task4
Idea full text search shortcut ctr+shift+f failure problem
Cadre de logback
2022-02-10 introduction to the design of incluxdb storage engine TSM
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
2022-01-27 research on the minimum number of redis partitions
Sitescms v3.0.2 release, upgrade jfinal and other dependencies
18W word Flink SQL God Road manual, born in the sky
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
Sword finger offer 11 Rotate the minimum number of the array
2022-01-27 redis cluster cluster proxy predixy analysis
Anan's doubts
Sword finger offer 17 Print from 1 to the maximum n digits