当前位置:网站首页>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;
}
}
}
边栏推荐
- Cyber Nuwa, how to make digital people?
- High number_ Chapter 4__ curvilinear integral
- How to select reliable securities analysts?
- Solana's "deceptive behavior": making mobile phones and opening stores
- Solana「迷惑行为」:造手机、开门店
- Kingbasees Security Guide for Jincang database -- 5.1. database transmission security
- H265/hevc noun explanation -- CTU, CTB, Cu, CB, Tu, PU, TB, Pb, LCU, slice, tile, chroma, luma, I frame, B frame, P frame
- Kingbasees security guide of Jincang database -- 6.2. Configuration files related to authentication
- MySQL: data types and operators
- C语言初阶——循环语句(while,for,do while)
猜你喜欢

Information system project manager (2022) - key content: Project Contract Management (13)

VAE generation model (with VAE implementation MNIST code)

pl/sql之各参数详解(“箱子模型“)

idea启动项目mvn命令终端用不了法将“mvn”项识别为 cmdlet

【二、移动web网页开发】2D&3D转换与动画、移动端布局、响应式布局

RT-Thread改变打印串口(在BSP的基础上添加其他功能)

null安全与异常

Esp8266 WiFi module and mobile communication

Glusterfs file is not mounted, permission: R-S

网页源代码查看竟然有这么多方法!你都知道吗?
随机推荐
Information system project manager (2022) - key content: Knowledge Management (15)
【day03】流程控制语句
About me writing a custom cell
25 openwrt guest network add
22 openwrt uses external kernel and kernel_ config
Practice and thinking of AI standardization engine in pink client
26 openwrt port forwarding DMZ UPnP
Kotlin -- function
Idea2022 change the local warehouse and configure Alibaba cloud central warehouse
21 openwrt kernel module changed to.Ko automatic loading
After login, the upper right corner changes to enter the login status
重要的 SQL Server 函数 - 日期函数
10 more advanced open source command line tools
Reading of the paper "attentional encoder network for targeted sentimental classification"
CMake使用基础汇总
写给学生的一点建议-如何构建自己的知识体系?
Cloud native Devops status survey questionnaire solicitation: kodelurover launched jointly with oschina
Docking with Hang Seng express ― dolphin DB NSQ plug-in tutorial
Kingbasees Security Guide for Jincang database -- 6.1 introduction to strong authentication
Kingbasees Security Guide for Jincang database -- 4 data access protection