当前位置:网站首页>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 .
边栏推荐
- 【Hot100】3. 无重复字符的最长子串
- [golang] how to install iris
- 【TcaplusDB知识库】TcaplusDB限制条件介绍
- 3. Caller 服务调用 - dapr
- 使用Karmada实现Helm应用的跨集群部署
- WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)
- 你好,现在网上炒股开户买股票安全吗?
- 基数排序——【常见排序法(2/8)】
- Slim GAIN(SGAIN)介绍及代码实现——基于生成对抗网络的缺失数据填补
- Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
猜你喜欢

Redmibook Pro 14 enhanced version cannot open delta software drastudio_ v1.00.07.52
![[proteus simulation] L297 driving stepping motor](/img/12/7902cf31f19df5d2613de7f25dca5b.png)
[proteus simulation] L297 driving stepping motor
![[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels](/img/35/63c9793ec7bc1c90c759504e84dc96.png)
[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels

浅谈 SAP 软件里的价格折扣设计原理

#夏日挑战赛#OHOS构建自定义服务实战

QQ appears large-scale number theft, why is this? Is there no solution?

2022年暑期及9月份CSP-J1 CSP-S1初赛 培训计划及学习要点

Use open connector to integrate hubspot and SAP systems

The future of platform as code is kubernetes extension

知道这几个命令让你掌握Shell自带工具
随机推荐
2022年暑期及9月份CSP-J1 CSP-S1初赛 培训计划及学习要点
Tiktok actual battle ~ list of bloggers I follow, follow and check
简单介绍一下tensorflow与pytorch的相互转换(主要是tensorflow转pytorch)
昨日元宇宙|Meta “元宇宙”部门一季度亏损29.6亿美元,六福珠宝发行数字藏品
【杂谈】2021/01/31 哦豁
GCC efficient graph revolution for joint node representationlearning and clustering
Kiss in the metauniverse! CMU launched VR head display plug-in, reproducing the vivid touch of lips
【Golang】安装 iris 的方法
Introduction to reverse commissioning PE structure details 02/07
js中订阅发布模式bus
Why MySQL table connection is faster than subquery
How to log in to your WordPress admin dashboard
Zuckerberg to investors: don't expect anything from metauniverse
Slim gain (sgain) introduction and code implementation -- missing data filling based on generated countermeasure network
【Hot100】3. 无重复字符的最长子串
WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)
REDIS00_ Explain redis Conf configuration file
[tcapulusdb knowledge base] Introduction to tcapulusdb restrictions
[redis] a brief summary of redis on January 31, 2021 No.01
How to clear the cache in WordPress