当前位置:网站首页>Cardinality sorting - common sorting method (2/8)
Cardinality sorting - common sorting method (2/8)
2022-06-28 16:43:00 【Dongguan hehe】
Catalog
2、 The process of sorting cardinality
1、 What is cardinal sort
Cardinal sort is a non comparative sort , The common comparison sort is bubbling , Directly inserted into the , Wait . These sorts require comparison and exchange to sort the data . Instead of the comparison sort, there is usually a cardinal sort , Count sorting , Bucket sorting, etc . These sorts do not need to be compared . The specific steps are as follows :
2、 The process of sorting cardinality
1、 step
We need to establish 10 A queue ( Arrays can also ), Respectively from the 0 Number to 9. Then the data to be sorted According to the position The numbers of are stored in the queue respectively . After all are queued , From number 0 The queue of starts to get out of the queue . After the same reason According to ten , Hundred bit , Thousands are queued , And out of the line . Until it reaches the highest point in this set of data .
2、 Picture demonstration
We give a set of data as follows : The data is out of order .
1、 Enter the queue by single digit word

2、 Data after leaving the queue

3、 Enter the queue by ten digits

4、 Outgoing queue

5、 Enter the queue by hundreds

6、 Outgoing queue

7、 In thousands ( highest ) Numbers are queued

8、 Outgoing queue , That is, orderly data

3、 Code implementation
import java.util.concurrent.ArrayBlockingQueue;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Dongguan hehe
* Date:2022-06-23
* Time:15:24
*/
public class RadixSort {
public static void radixSort(int[] array) {
// Create ten buckets ( queue )
List<Queue<Integer>> queueList = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
queueList.add(new ArrayBlockingQueue<Integer>(1000));
}
// Find the maximum value in the array
int max = array[0];
for (int k : array) {
if (max < k) {
max = k;
}
}
// Record the maximum number of digits
int count = 0;
while (max != 0) {
count++;
max /= 10;
}
// Start sorting
//x Represents the current base digit
int x = 0;
while (x < count) {
for (int i = 0; i < array.length; i++) {
int num = getFigure(array[i], x);
queueList.get(num).offer(array[i]);
}
int j = 0;
for (int i = 0; i < array.length; i++) {
if (j <= 9 && queueList.get(j).isEmpty()) {
j++;
// Because the current queue is empty , You need to skip this cycle i++, But at this time i Situated array[i] No assignment , So we need to i--
i--;
continue;
}
array[i] = queueList.get(j).poll();
}
x++;
}
}
/**
* Back to page k What is the value of the bit (k=0,1,2)( individual , Ten , hundred )
* @param i
* @param k
* @return
*/
public static int getFigure(int i, int k) {
int arr[] = {1, 10, 100, 1000, 10000, 100000, 1000000};
return (i / arr[k]) % 10;
}
public static void main(String[] args) {
int[] arr = {65, 43, 5, 7, 4, 5, 76, 23, 4, 765, 42, 6, 7, 8};
radixSort(arr);
System.out.println(Arrays.toString(arr));
}
}If we have a large amount of data to be queued, we can increase the size of the created queue , If the data to be scheduled is large, it can be changed getFigure Data in .
Of course, you can also use arrays to implement , If you use an array to implement it, you should pay attention to the first in first out array .
边栏推荐
- Interviewer: how does the thread pool reuse threads? Do you know? Tell me about it
- The future of platform as code is kubernetes extension
- 【Hot100】3. 无重复字符的最长子串
- 媒体数据处理V2版本(VPC)图像缩放内容解析
- CODING DevOps 助力中化信息打造新一代研效平台,驱动“线上中化”新未来
- NOIP1998-2018 CSP-S2 2019 2021提高组解题报告与视频
- 【力扣】977. 有序数组的平方
- Knowing these commands allows you to master shell's own tools
- 元宇宙中能接吻了!CMU推出VR头显外挂,复刻唇部逼真触觉
- Tongziping, partner of Tongchuang Weiye: "what should yuan universe invest in?"
猜你喜欢

10.hystrix circuit breaker

Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager

Cloud sports, 360 ° witnessing speed and passion

CRM 全栈开发工具 WebClient UI Workbench 的设计细节介绍

Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills

AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里

【Hot100】4. 寻找两个正序数组的中位数

【Hot100】4. Find the median of two positive arrays

General solution of island problems and DFS framework

2022年暑期及9月份CSP-J1 CSP-S1初赛 培训计划及学习要点
随机推荐
LDD knowledge sorting
Solve the problem that subcomponents will not be destroyed through setTimeout
机器学习之卷积神经网络使用cifar10数据集和alexnet网络模型训练分类模型,安装labelimg,以及报错ERROR
Mysql自连接查询「建议收藏」
知道这几个命令让你掌握Shell自带工具
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
Traffic management and control of firewall Foundation
Why MySQL table connection is faster than subquery
Cross cluster deployment of helm applications using karmada
一台服务器最大并发 tcp 连接数多少?65535?
【Hot100】4. Find the median of two positive arrays
运动App如何实现端侧后台保活,让运动记录更完整?
Yesterday, metauniverse | Wal Mart set up an innovation department to explore metauniverse and Web3, and Dior released the metauniverse Exhibition
Cross cluster deployment of helm applications using karmada
WPF video hard decoding, rendering and playing (no airspace) (support 4K, 8K and high frame rate video)
IPDK — Overview
ID card copy tutorial (use t5577 card to copy 4100 card)
Convolutional neural network for machine learning uses cifar10 data set and alexnet network model to train classification model, install labelimg, and report error
On the design principle of price discount in SAP software
How to log in to your WordPress admin dashboard