当前位置:网站首页>Bucket sort
Bucket sort
2022-06-11 09:36:00 【hotarugali】
【 notes 】 Refer to the textbook 「 Introduction to algorithms 」.
1. brief introduction
Bucket sorting is to divide the sequence to be sorted into a limited number of buckets , Then sort each bucket separately . The premise of bucket sorting is that the keyword values of the sorted sequence conform to uniform distribution , At this time, the average time complexity of bucket sorting is O(n + {n^2 \over k} + k), The worst time complexity is O(n^2), among k For the number of barrels . When the number of barrels k \approx n when , At this point, the complexity of bucket sorting is linear O(n) .
The order of barrels is Non original , Its The stability depends on the stability of the inner ordering . Generally, stable insertion sort is used as the inner sort algorithm , At this time, the bucket sorting is stable .
2. thought
The main idea of bucket sorting is to block the key values of the sorting sequence , Each piece corresponds to a barrel , Then use insert sort for each bucket ( Or other sorting algorithms ) Sort , Finally, all the elements in the bucket are connected in series to get an ordered sequence .
3. Realization
3.1 Pseudo code
BucketSort(A, mx, n) {
// mx Is the maximum value ,n Is the number of barrels
// Definition n A barrel
define bucket[n]
// Calculate the block size of the block
bucketsize = mx/n + 1
// It is divided into blocks according to the sorting key values of the elements
for i = 1 to A.length
bucket[A[i]/bucketsize].PushBack(A[i])
for i = 0 to n-1
InsertSort(bucket[i])
cnt = 0
for i = 0 to n-1
for j = 1 to bucket[i].length
A[++cnt] = bucket[i][j]
}3.2 Templates
#include <bits/stdc++.h>
using namespace std;
#ifndef _BUCKET_
#define _BUCKET_
#define ll int
#define MAXN 100005
#define MAXBUK 1000
#define INF 10000000
// Bucket sort
template < typename T >
struct Bucket {
vector < pair<T, ll> > bucket[MAXN];
Bucket() {}
// Insertion sort
void insertSort(vector < pair<T, ll> > & bkt) {
for(ll i = 1; i < bkt.size(); ++i) {
pair <T, ll> key = bkt[i];
ll j = i - 1;
while(~j && bkt[j].second > key.second) {
bkt[j+1] = bkt[j];
j--;
}
bkt[j+1] = key;
}
}
// Bucket sort
void bucketSort(T *bA, T *eA, ll *bK, ll *eK, ll mx = INF, ll bn = MAXBUK) {
ll len_A = eA - bA;
ll len_K = eK - bK;
T *A = bA;
ll *K = bK;
ll bucketsize = mx/bn + 1;
// Judge whether the size of keyword array matches the size of element array
assert(len_A == len_K);
// Initialize bucket
for(ll i = 0; i < bn; ++i) {
bucket[i].clear();
}
// Put the elements in the bucket
for(ll i = 0; i < len_K; ++i) {
bucket[K[i]/bucketsize].push_back(pair<T, ll>{A[i], K[i]});
}
// Insert sorting for each bucket
ll cnt = 0;
for(ll i = 0; i < bn; ++i) {
insertSort(bucket[i]);
for(ll j = 0; j < bucket[i].size(); ++j) {
A[cnt++] = bucket[i][j].first;
}
}
}
};
#endif边栏推荐
- ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用
- 远程办公最佳实践及策略
- Before applying data warehouse ODBC, you need to understand these problems first
- ERP体系的这些优势,你知道吗?
- OpenCV CEO教你用OAK(四):创建复杂的管道
- 报错ModularNotFoundError: No module named ‘find_version’
- Flask (VI) - template
- OpenSSL usage
- Machine learning notes - the story of master kaggle Janio Martinez Bachmann
- 服装ERP:企业如何进行施行规划?
猜你喜欢

报错Output image is bigger(1228800B) than maximum frame size specified in properties(1048576B)

Technical practice of dolphin dispatching in kubernetes system

【ROS】noedic-moveit安装与UR5模型导入

关于原型及原型链

Machine learning notes - in depth Learning Skills Checklist

报错ModularNotFoundError: No module named ‘find_version’

MSF SMB based information collection

机器学习笔记 - 使用TensorFlow的Spatial Transformer网络

Talk about reading the source code

ES6新增特性--箭头函数
随机推荐
Résumé de la méthode d'examen des mathématiques
Openstack explanation (XXIII) -- other configurations, database initialization and service startup of neutron
关于原型及原型链
Zhiyun health submitted the statement to HKEx again: the loss in 2021 exceeded 4billion yuan, an increase of 43% year-on-year
Install jupyter in the specified environment
Don't use redis list to implement message queue. Stream is designed for queues
【分享】企业如何进行施行规划?
1400. construct K palindrome strings
What problems can ERP system help enterprises deal with?
【服装ERP】施行在项目中的重要性
Technical practice of dolphin dispatching in kubernetes system
实现边充边OTG的PD芯片GA670-10
Pulsar job Plaza | Tencent, Huawei cloud, shrimp skin, Zhong'an insurance, streamnational and other hot jobs
document对象
Day41 process pool and thread pool
【软件】ERP体系价值最大化的十点技巧
[scheme design] scheme of household oximeter based on single chip microcomputer
从企业评价的方历来看ERP软件成功与失利
[intelligent development] scheme design and hardware development of sphygmomanometer
Design of wrist sphygmomanometer based on sic32f911ret6