当前位置:网站首页>基数排序——【常见排序法(2/8)】
基数排序——【常见排序法(2/8)】
2022-06-28 16:11:00 【东莞呵呵】
目录
1、什么是基数排序
基数排序是一种非比较排序,常见的比较排序一般有冒泡,直接插入,快排等等。这些排序都需要进行比较交换才能进行对于数据的排序。而非比较排序一般有基数排序,计数排序,桶排序等等。这些排序是不需要进行比较的。具体步骤如下:
2、基数排序的过程
1、步骤
我们需要创建10个队列(数组也可以),分别从0编号到9。然后将待排序数据按照个位的数字分别存入队列中。全部入队列之后,在从编号0的队列开始出队列。同理之后按照十位,百位,千位入队列,再出队列。直到达到这组数据中的最高位。
2、图片演示
我们给出一组数据如下:此时的数据是无序的。
1、按照个位数字入队列

2、出队列之后的数据

3、按照十位数字入队列

4、出队列

5、按照百位数字入队列

6、出队列

7、按照千位(最高位) 数字入队列

8、出队列,即是有序的数据

3、代码实现
import java.util.concurrent.ArrayBlockingQueue;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 东莞呵呵
* Date:2022-06-23
* Time:15:24
*/
public class RadixSort {
public static void radixSort(int[] array) {
//创建十个桶(队列)
List<Queue<Integer>> queueList = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
queueList.add(new ArrayBlockingQueue<Integer>(1000));
}
//找出数组中的最大值
int max = array[0];
for (int k : array) {
if (max < k) {
max = k;
}
}
//记录最大值的位数
int count = 0;
while (max != 0) {
count++;
max /= 10;
}
//开始排序
//x代表当前的基数位
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++;
//因为当前队列为空时,需要跳过这次循环后i++,但是此时i处的array[i]没有赋值,所以要i--
i--;
continue;
}
array[i] = queueList.get(j).poll();
}
x++;
}
}
/**
* 返回第k位的值是几(k=0,1,2)(个,十,百)
* @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));
}
}如果我们待排的数据较多时可以增大创建队列的大小,如果待排的数据比较大则可以改变getFigure中的数据。
当然也可以使用数组来实现,用数组来实现的话就要注意先进数组的元素先出数组。
边栏推荐
- Etcd visualization tool: an introduction to kstone (I)
- LDD 知识整理
- Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
- Super automation and the future of network security
- Design details of the full stack CRM development tool webclient UI workbench
- CODING DevOps 助力中化信息打造新一代研效平台,驱动“线上中化”新未来
- js中订阅发布模式bus
- Hello, is it safe to open an account to buy stocks online?
- Interview with wangyuntao of China Academy of information technology: digital and real integration enables the prosperity and development of cultural industry
- Kiss in the metauniverse! CMU launched VR head display plug-in, reproducing the vivid touch of lips
猜你喜欢

【TcaplusDB知识库】TcaplusDB技术支持介绍

Knowing these commands allows you to master shell's own tools

10: 00 interview, came out at 10:02, the question is really too

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

知道这几个命令让你掌握Shell自带工具

Cross cluster deployment of helm applications using karmada

24岁秃头程序员教你微服务交付下如何持续集成交付,学不会砍我

3. caller service call - dapr

Redmibook Pro 14 enhanced version cannot open delta software drastudio_ v1.00.07.52

The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
随机推荐
China energy integration and Tianyi cloud create an "energy brain"
O & M - unified gateway is very necessary
3. Caller 服务调用 - dapr
Hello, is it safe to open an account to buy stocks online?
Cloud sports, 360 ° witnessing speed and passion
【Hot100】3. Longest substring without duplicate characters
论文解读(GCC)《Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering》
2022年暑期及9月份CSP-J1 CSP-S1初赛 培训计划及学习要点
面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
FS2K人脸素描属性识别
Focus on the 35 year old Kan: fear is because you don't have the ability to match your age
NOIP2011-2018提高组解题报告
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
The sadness of software testers is Their own technical ability can not meet the requirements of large manufacturers?
云上竞技,360°见证速度与激情
如何查询数据库中一个表中的所有数据呢?
开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
同创伟业合伙人童子平:“元宇宙”究竟该投什么
Mysql自連接查詢「建議收藏」