当前位置:网站首页>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();
}
}
边栏推荐
- 想请教一下,究竟有哪些劵商推荐?手机开户是安全么?
- 证券如何在线开户?手机开户是安全么?
- Adding data to the head or tail of the rar file can still decompress normally
- [dynamic planning] p1220: interval DP: turn off the street lights
- Research Report on micro vacuum pump industry - market status analysis and development prospect prediction
- [fluent] dart generic (generic class | generic method | generic with specific type constraints)
- Check the confession items of 6 yyds
- Codeworks global round 19 (CF 1637) a ~ e problem solution
- Unexpectedly, there are such sand sculpture code comments! I laughed
- Get weekday / day of week for datetime column of dataframe - get weekday / day of week for datetime column of dataframe
猜你喜欢

Roommate, a king of time, I took care of the C language structure memory alignment

I drew a Gu ailing with characters!
![[fluent] dart technique (independent main function entry | nullable type determination | default value setting)](/img/cc/3e4ff5cb2237c0f2007c61db1c346d.jpg)
[fluent] dart technique (independent main function entry | nullable type determination | default value setting)

qwb2018_ core kernel_ rop

AMD's largest transaction ever, the successful acquisition of Xilinx with us $35billion

Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve

kernel tty_ struct

Add two numbers of leetcode
![[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)

Research Report on ranking analysis and investment strategic planning of RFID market competitiveness of China's industrial manufacturing 2022-2028 Edition
随机推荐
Interested parties add me for private chat
Research Report on the overall scale, major manufacturers, major regions, products and applications of outdoor vacuum circuit breakers in the global market in 2022
ctf-HCTF-Final-Misc200
Construction and maintenance of business websites [4]
How to open an account online? Is it safe to open a mobile account?
Analysis of enterprise financial statements [1]
Research Report on the overall scale, major manufacturers, major regions, products and applications of swivel chair gas springs in the global market in 2022
This team with billions of data access and open source dreams is waiting for you to join
One week dynamics of dragon lizard community | 2.07-2.13
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
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second
Accounting regulations and professional ethics [18]
Go web programming practice (2) -- process control statement
Don't you want to have a face-to-face communication with cloud native and open source experts? (including benefits
Go language learning summary (5) -- Summary of go learning notes
Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve
China microporous membrane filtration market trend report, technological innovation and market forecast
Download vagrant box file locally from Atlas and configuring it
Longest public prefix of leetcode