当前位置:网站首页>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();
}
}
边栏推荐
- Internal/validators js:124 throw new ERR_ INVALID_ ARG_ Type (name, 'string', value) -- solution
- 3DES (deSede) encryption CBC mode pkcs7padding filling Base64 encoding key 24byte iv8byte
- Common routines of compressed packets in CTF
- Add two numbers of leetcode
- Micro SD Card Industry Research Report - market status analysis and development prospect forecast
- AMD's largest transaction ever, the successful acquisition of Xilinx with us $35billion
- Accounting regulations and professional ethics [16]
- Friends who firmly believe that human memory is stored in macromolecular substances, please take a look
- Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
- qwb2018_ core kernel_ rop
猜你喜欢
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
Talk about macromolecule coding theory and Lao Wang's fallacy from the perspective of evolution theory
Common routines of compressed packets in CTF
Go language learning summary (5) -- Summary of go learning notes
Customized Huawei hg8546m restores Huawei's original interface
[question brushing diary] classic questions of dynamic planning
kernel tty_ struct
I drew a Gu ailing with characters!
[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second
Write the content into the picture with type or echo and view it with WinHex
随机推荐
证券如何在线开户?手机开户是安全么?
Is it safe to buy funds on securities accounts? Where can I buy funds
[12] the water of the waves is clear, which can wash my tassel. The water of the waves is muddy, which can wash my feet
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
2021 v+ Quanzhen internet global innovation and Entrepreneurship Challenge, one of the top ten audio and video scene innovation and application pioneers
2021 software security report: open source code, happiness and disaster depend on each other?
Research Report on the overall scale, major manufacturers, major regions, products and applications of building automation power meters in the global market in 2022
Backpack template
Accounting regulations and professional ethics [19]
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of precoated metallic coatings in the global market in 2022
Unexpectedly, there are such sand sculpture code comments! I laughed
Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
Web3js method to obtain account information and balance
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
Record the problems encountered by nodejs asynchronism
Hot backup routing protocol (HSRP)
I would like to ask what securities dealers recommend? Is it safe to open a mobile account?
1007 maximum subsequence sum (25 points) "PTA class a exercise"
在券商账户上买基金安全吗?哪里可以买基金
Construction and maintenance of business websites [9]