当前位置:网站首页>基数排序——【常见排序法(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中的数据。
当然也可以使用数组来实现,用数组来实现的话就要注意先进数组的元素先出数组。
边栏推荐
- Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
- C#/VB.NET 将PDF转为Excel
- 薅羊毛的机会了,点个“赚”即有机会赚取高额佣金
- General solution of island problems and DFS framework
- Zuckerberg to investors: don't expect anything from metauniverse
- Super automation and the future of network security
- NOIP1998-2018 CSP-S2 2019 2021提高组解题报告与视频
- [random talk] January 31, 2021 Oh Huo
- Tongziping, partner of Tongchuang Weiye: "what should yuan universe invest in?"
- The future of platform as code is kubernetes extension
猜你喜欢

RedmiBook Pro 14增强版 打不开台达软件DRAStudio_v1.00.07.52

O & M - unified gateway is very necessary
![[MySQL] official website document learning query statement SQL precautions](/img/aa/bf27b609e2fda1edaa46f134fc5626.png)
[MySQL] official website document learning query statement SQL precautions

The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure

运维-- 统一网关非常必要

【Hot100】1. Sum of two numbers

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

Fs2k face sketch attribute recognition

Briefly introduce the conversion between tensorflow and pytorch (mainly tensorflow to pytorch)

Tiktok actual battle ~ list of bloggers I follow, follow and check
随机推荐
运动App如何实现端侧后台保活,让运动记录更完整?
[force button] 977 Square of ordered array
Internet of things cloud convergence Security Guide
昨日元宇宙|Meta “元宇宙”部门一季度亏损29.6亿美元,六福珠宝发行数字藏品
【Hot100】4. 寻找两个正序数组的中位数
LDD 知识整理
#夏日挑战赛#OHOS构建自定义服务实战
Interview with wangyuntao of China Academy of information technology: digital and real integration enables the prosperity and development of cultural industry
PID control details [easy to understand]
The sadness of software testers is Their own technical ability can not meet the requirements of large manufacturers?
AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
C#/VB.NET 将PDF转为Excel
【Hot100】3. 无重复字符的最长子串
Kiss in the metauniverse! CMU launched VR head display plug-in, reproducing the vivid touch of lips
【TcaplusDB知识库】TcaplusDB限制条件介绍
NOIP1998-2018年普及组 CSP-J2 2019 2020 解题报告及视频
Media data processing V2 Version (VPC) image zoom content analysis
10 years of testing experience, worthless in the face of the physiological age of 35
Can SQL queries be used in the tablestore to find out all the data in the table?