当前位置:网站首页>Cardinality sorting (detailed illustration)
Cardinality sorting (detailed illustration)
2022-07-02 21:28:00 【S dream chasing boy】
One 、 What is cardinal sort
(1) The value of each bit through the key value , Assign the elements to be sorted to some buckets , To achieve the function of sorting
(2) Radix sort is a sort of stability , Cardinal ranking method is a stable ranking method with high efficiency
(3) Cardinality sorting is an extension of bucket sorting
Two 、 Realization principle
Compare the values to be compared ( Natural number ) Uniform to 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 .
3、 ... and 、 Implementation steps
(1) Determine the maximum number of elements in the array (MAX)( Determine the number of rounds performed )
(2) establish 0~9 A barrel ( At the bottom of the bucket is the queue ), Because all digital elements are made of 0~9 Ten numbers of
(3) Judge the bits of each element in turn , Ten to MAX position , Put it into the corresponding bucket , Out of the team , Save to the original array ; straight
to MAX Round end output array .
(4) The specific implementation steps are shown in the figure below

Four 、 Code implementation
import java.util.LinkedList;
public class Radix {
public static void main(String[] args) {
int[] arr = { 23, 1, 4, 9, 98, 132, 42 };
sort(arr);
}
public static void sort(int[] arr) {
// 1. look for classification - collect The number of rounds ( The length of the maximum )
int radix = getRadix(arr);
// 2. Creating buckets list Collection of all barrels Each barrel is LinkedList Use it as a queue
LinkedList<Integer>[] list = new LinkedList[10];
for (int i = 0; i < list.length; i++) {
list[i] = new LinkedList<>();
}
// 3. Start classification - collect
for (int r = 1; r <= radix; r++) {
// The classification process
for (int i = 0; i < arr.length; i++) {
list[getIndex(arr[i], r)].offer(arr[i]);
}
int index = 0; // Traverse arr Original array
// The process of collecting
for (int i = 0; i < list.length; i++) {
while (!list[i].isEmpty()) {
arr[index++] = list[i].poll();
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
private static int getIndex(int num, int r) {
int ret = 0;
for (int i = 1; i <= r; i++) {
ret = num % 10;
num /= 10;
}
return ret;
}
private static int getRadix(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return (max + "").length();
}
}
边栏推荐
- Lantern Festival, come and guess lantern riddles to win the "year of the tiger Doll"!
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of signal distributors in the global market in 2022
- [fluent] dart generic (generic class | generic method | generic with specific type constraints)
- 2021 v+ Quanzhen internet global innovation and Entrepreneurship Challenge, one of the top ten audio and video scene innovation and application pioneers
- mysql
- BitSet complement
- JDBC | Chapter 3: SQL precompile and anti injection crud operation
- China's noise meter market trend report, technical dynamic innovation and market forecast
- Accounting regulations and professional ethics [16]
- Golang string segmentation
猜你喜欢

kernel_ uaf

kernel tty_ struct

qwb2018_ core kernel_ rop

rwctf2022_ QLaaS

Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve
![[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)](/img/04/4070d51ce8b7718db609ef2fc8bcd7.jpg)
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
![[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)](/img/a7/0b87fa45ef2edd6fac519b40adbeae.gif)
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)

Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
![[dynamic planning] p1220: interval DP: turn off the street lights](/img/b6/405e29ca88fac40caee669a3b7893f.jpg)
[dynamic planning] p1220: interval DP: turn off the street lights

AMD's largest transaction ever, the successful acquisition of Xilinx with us $35billion
随机推荐
5 environment construction spark on yarn
Analysis of enterprise financial statements [2]
Construction and maintenance of business websites [4]
Research Report on market supply and demand and strategy of China's plastic trunking industry
China plastic bottle market trend report, technological innovation and market forecast
A river of spring water flows eastward
Go cache of go cache series
Who do you want to open a stock account? Is it safe to open a mobile account?
AES encryption CBC mode pkcs7padding filling Base64 encoding key 32byte iv16byte
[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
Makefile: usage of control functions (error, warning, info)
Research Report on plastic antioxidant industry - market status analysis and development prospect forecast
BitSet complement
Research Report on the overall scale, major manufacturers, major regions, products and applications of outdoor vacuum circuit breakers in the global market in 2022
Add two numbers of leetcode
[fluent] dart generic (generic class | generic method | generic with specific type constraints)
Research Report on micro vacuum pump industry - market status analysis and development prospect prediction
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
Plastic granule Industry Research Report - market status analysis and development prospect forecast
Accounting regulations and professional ethics [19]