当前位置:网站首页>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();
}
}
边栏推荐
- Spend more time with your computer on this special holiday, HHH
- mysql
- [shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)
- [fluent] dart generic (generic class | generic method | generic with specific type constraints)
- China's log saw blade market trend report, technological innovation and market forecast
- Unexpectedly, there are such sand sculpture code comments! I laughed
- Codeworks global round 19 (CF 1637) a ~ e problem solution
- Research Report on the overall scale, major manufacturers, major regions, products and applications of swivel chair gas springs in the global market in 2022
- Common routines of compressed packets in CTF
- Select function
猜你喜欢

Unexpectedly, there are such sand sculpture code comments! I laughed
![[question brushing diary] classic questions of dynamic planning](/img/31/fcd8230f809d6178f11e7095c1ef94.jpg)
[question brushing diary] classic questions of dynamic planning

Add two numbers of leetcode

Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition

In depth research and investment feasibility report of global and Chinese isolator industry, 2022-2028

Talk about macromolecule coding theory and Lao Wang's fallacy from the perspective of evolution theory
![[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)](/img/04/4070d51ce8b7718db609ef2fc8bcd7.jpg)
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)

Review of the latest 2022 research on "deep learning methods for industrial defect detection"

Interested parties add me for private chat
![[dynamic planning] p1220: interval DP: turn off the street lights](/img/b6/405e29ca88fac40caee669a3b7893f.jpg)
[dynamic planning] p1220: interval DP: turn off the street lights
随机推荐
Research Report on the overall scale, major manufacturers, major regions, products and applications of swivel chair gas springs in the global market in 2022
Accounting regulations and professional ethics [17]
Plastic granule Industry Research Report - market status analysis and development prospect forecast
One week dynamics of dragon lizard community | 2.07-2.13
[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second
[fluent] dart technique (independent main function entry | nullable type determination | default value setting)
Go web programming practice (2) -- process control statement
This team with billions of data access and open source dreams is waiting for you to join
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
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of signal distributors in the global market in 2022
Unexpectedly, there are such sand sculpture code comments! I laughed
Import a large amount of data to redis in shell mode
China microporous membrane filtration market trend report, technological innovation and market forecast
Research Report on the overall scale, major manufacturers, major regions, products and applications of sliding door dampers in the global market in 2022
Research Report on minimally invasive medical robot industry - market status analysis and development prospect prediction
Talk about macromolecule coding theory and Lao Wang's fallacy from the perspective of evolution theory
Who do you want to open a stock account? Is it safe to open a mobile account?
Research Report on market supply and demand and strategy of China's atomic spectrometer industry
Construction and maintenance of business websites [7]