当前位置:网站首页>基数排序——【常见排序法(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中的数据。
当然也可以使用数组来实现,用数组来实现的话就要注意先进数组的元素先出数组。
边栏推荐
- Can SQL queries be used in the tablestore to find out all the data in the table?
- PostgreSQL exception handling
- 【208】基于AccessToken方式实现API设计
- Tiktok actual battle ~ list of bloggers I follow, follow and check
- 昨日元宇宙| 沃尔玛成立探索元宇宙和Web3的创新部门,Dior发布元宇宙展览
- MySQL self connection query "suggestions collection"
- tablestore中可以使用sql查询可以查出表中所有的数据吗?
- How can the sports app keep the end-to-side background alive to make the sports record more complete?
- Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
- 使用 Open Connector 进行 HubSpot 和 SAP 系统的集成工作
猜你喜欢

Big God explains open source buff gain strategy live lecture

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

10.Hystrix断路器

IPDK — Overview

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

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

#夏日挑战赛#OHOS构建自定义服务实战
![[MySQL] official website document learning query statement SQL precautions](/img/aa/bf27b609e2fda1edaa46f134fc5626.png)
[MySQL] official website document learning query statement SQL precautions

The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10

Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
随机推荐
Code implementation of gain (4) -- gap dataset missing data filling based on GaN (sequence) [improved version]
论文解读(GCC)《Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering》
扎克伯格致投资者:不要对元宇宙有任何期待
Summer Challenge ohos build custom service practice
如何根据多元索引查询最后一条数据,达到 sql order by desc limit 1的效果呢?
Design details of the full stack CRM development tool webclient UI workbench
Focus on the 35 year old Kan: fear is because you don't have the ability to match your age
[random talk] January 31, 2021 Oh Huo
Stm32cubemx usage and function introduction
简单介绍一下tensorflow与pytorch的相互转换(主要是tensorflow转pytorch)
Media data processing V2 Version (VPC) image zoom content analysis
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
How can the sports app keep the end-to-side background alive to make the sports record more complete?
10.Hystrix断路器
平台即代码的未来是Kubernetes扩展
The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure
【Hot100】2.两数相加
数字藏品热潮之下,你必须知道的那些事儿
Change exchange (dynamic planning)
WPF video hard decoding, rendering and playing (no airspace) (support 4K, 8K and high frame rate video)