当前位置:网站首页>Sort - cardinal sort
Sort - cardinal sort
2022-07-28 04:28:00 【Jiang CI ࿐】
Radix sorting ( Bucket sort ) Introduce :
Radix sorting (radix sort) Belong to “ Distributive sort ”(distribution sort), also called “ Barrel method ”(bucket sort) or bin sort, seeing the name of a thing one thinks of its function , It is through the value of each bit of the key value , Assign elements to be sorted to some “ bucket ” in , To achieve the function of sorting
Radix sort is a sort of stability , Cardinality sorting is a stable sorting method with high efficiency
Radix sorting (Radix Sort) It's an extension of bucket sort
The basic idea of Radix sorting
Unify all values to be compared into the same digit length , A shorter digit is zeroed . then , Let's start at the lowest point , Sort them one at a time . In this way, from the lowest ranking to the completion of the highest ranking , The sequence becomes an ordered sequence .
Code implementation
import java.util.Arrays;
public class practice {
public static void main(String[] args) {
int[] arr = new int[] {
12,98,100,123,1,155,26,30,87,96,130,97,65,88,79};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int[] arr) {
// Defining buckets
int[][] bucket = new int[10][arr.length];
// Define the bucket recorder
int[] elementCounts = new int[10];
// Find the maximum value in the array
int max = arr[0];
for(int i = 0;i<arr.length;i++) {
if(arr[i] > max) {
max = arr[i];
}
}
// Find the number of digits of the maximum value
int maxLength = (max + "").length();
int n = 1;
for(int h = 0;h<maxLength;h++) {
// Put data into the bucket
for(int i = 0;i<arr.length;i++){
// Calculate the value of a bit
int element = arr[i] / n % 10;// Put into the bucket
int count = elementCounts[element];
bucket[element][count] = arr[i];
elementCounts[element] = elementCounts[element] + 1;
}
int index= 0;
// Take out the data in the bucket
for(int k = 0;k<elementCounts.length;k++){
if(elementCounts[k] !=0){
for(int l = 0;l<elementCounts[k];l++){
arr[index] = bucket[k][l];
index++;
}
}
// Empty the bucket recorder
elementCounts[k] = 0;
}
n = n * 10;
}
}
}
边栏推荐
- The simulation test disconnects the server from the public network
- Use Baidu developer tool 4.0 to build a dedicated applet IDE
- 【伸手党福利】微信中h5网页调起扫一扫最简单的方法
- [performance optimization methodology series] III. core idea of performance optimization (2)
- CMake使用基础汇总
- About me writing a custom cell
- [Niuke] find 1+2+3+... +n
- 金仓数据库KingbaseES安全指南--5.2. 数据完整性保护
- Cyber Nuwa, how to make digital people?
- idea启动项目mvn命令终端用不了法将“mvn”项识别为 cmdlet
猜你喜欢

ESP8266 WIFI 模块和手机通信
![RuntimeError: stack expects each tensor to be equal size, but got [8] at entry 0 and [2] at entry 2](/img/66/27de1ac0f642fc91fca5196ea6e141.png)
RuntimeError: stack expects each tensor to be equal size, but got [8] at entry 0 and [2] at entry 2

The simulation test disconnects the server from the public network

《KG-BERT: BERT for Knowledge Graph Completion》
![[yolov5 practice 5] traffic sign recognition system based on yolov5 -yolov5 integration pyqt5](/img/81/89b8e38801f706ef396943a79ef4c5.png)
[yolov5 practice 5] traffic sign recognition system based on yolov5 -yolov5 integration pyqt5
![[coding and decoding] Huffman coding and decoding based on Matlab GUI [including Matlab source code 1976]](/img/af/27e3794f93166d8ecad9b42dafaf0f.png)
[coding and decoding] Huffman coding and decoding based on Matlab GUI [including Matlab source code 1976]

High number_ Chapter 4__ Curvilinear integral_ Exercise solution

Render the data obtained from the database to the table in elementui

Information system project manager (2022) - key content: Project Portfolio Management (19)

@Requiredargsconstructor annotation
随机推荐
【无标题】
【实战】使用 Web Animations API 实现一个精确计时的时钟
仿真测试断开服务器公网连接
Important SQL server functions - string utilities
High number_ Chapter 4__ Curvilinear integral_ Exercise solution
Learn regular expressions (regexp)
[record of question brushing] 9. Number of palindromes
Detailed explanation of pl/sql parameters ("box model")
H265/HEVC名词解释-- CTU,CTB,CU,CB,TU,PU,TB,PB,LCU,Slice,Tile,Chroma,Luma,I帧,B帧,P帧
Reading of papers on "towards generative aspect based sentimental analysis"
How much does it cost to build a self built server for ark survival evolution?
【day03】流程控制语句
[Niuke] find 1+2+3+... +n
pytorch_ Lightning in lightning_ The output of hparams.yaml in logs is null
ESP8266 WIFI 模块和手机通信
金仓数据库KingbaseES安全指南--5.1. 数据库的传输安全
Information system project manager (2022) - key content: Strategic Management (17)
Study notes of Gu Yujia on July 27, 2022
重要的 SQL Server 函数 - 其他函数
Information system project manager (2022) - key content: information system integrated testing and management, project management maturity model, quantitative project management (21)