当前位置:网站首页>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;
}
}
}
边栏推荐
- 登录之后右上角改变 进入登录状态
- Fearless of side impact damage, Chery arize 8 fully protects the safety of passengers
- 26 openwrt port forwarding DMZ UPnP
- "Three no's and five requirements" principle of enterprise Digitalization Construction
- Docking with Hang Seng express ― dolphin DB NSQ plug-in tutorial
- Kingbasees Security Guide for Jincang database -- 5.2. data integrity protection
- Kingbasees Security Guide for Jincang database -- 5.1. database transmission security
- [Niuke] find 1+2+3+... +n
- Kingbasees security guide of Jincang database -- 6.2. Configuration files related to authentication
- Important SQL server functions - date functions
猜你喜欢

Information system project manager (2022) - key content: Project Procurement Management (12)

重要的 SQL Server 函数 - 日期函数

【无标题】

23 openwrt switch VLAN configuration

Network visualization: features of convolution kernel and CNN visualization (through the attention part of gradient visualization network)

RN interface jump description

MySQL: data types and operators

高数_第4章__曲线积分_习题解法

H. 265 web player easyplayer realizes webrtc video real-time recording function

Full resolution of the use of go native plug-ins
随机推荐
Use Baidu developer tool 4.0 to build a dedicated applet IDE
Information system project manager (2022) - key content: Project Procurement Management (12)
Harmony's Application on the shelves reported an error. The solution of "please use the API of the released version to develop the application and apply for listing"
方舟生存进化自建服务器要多少成本?
RuntimeError: stack expects each tensor to be equal size, but got [8] at entry 0 and [2] at entry 2
[Niuke] find 1+2+3+... +n
Reading of papers on "towards generative aspect based sentimental analysis"
RT-Thread改变打印串口(在BSP的基础上添加其他功能)
Domestic high hidden free agent crawler code
Important SQL server functions - date functions
Seamless support for hugging face community, colossal AI low-cost and easy acceleration of large model
Ffmpeg common instructions
重要的 SQL Server 函数 - 日期函数
【牛客】求1+2+3+...+n
高数_第4章__曲线积分_习题解法
.net upload files through boundary
C语言初阶——循环语句(while,for,do while)
How to select reliable securities analysts?
MATLB | location and constant volume IEEE30 node implementation of distributed energy
The unsatisfied analysis of setup and hold timing is the solution