当前位置:网站首页>排序5-计数排序
排序5-计数排序
2022-07-28 15:27:00 【世_生】
前言

一、计数排序
思想:计数排序又称鸽巢原理,是对哈希表直接定址法变形的应用。操作步骤:
- 统计相同元素出现的个数
- 根据统计的结果将序列回收到原来的序列中
绝对映射:
细节:
(1):当数组中的最大值较大,且数组中的最小值也较大,那么我们开辟B空间,就会有很大的浪费。
所以相对映射来了:
代码:(几处细节的处理要看仔细)
void CountSort(int* a,int n)
{
// 选择出最大值,最小值
int i = 0,min = a[0],max = a[0];
for(i = 0;i < n;i++)
{
if(a[i] > max)
max = a[i];
if(a[i] < min)
min = a[i];
}
int range = max - min + 1;
int* count = (int*)malloc(sizeof(int) * range);
memset(count,0,sizeof(int) * range);
//计数
for(i = 0;i < n;i++)
{
count[a[i] -min]++;
}
int j = 0;
//排序
for(i = 0;i < range;i++)
{
while(count[i]--)
{
a[j++] = i + min;
}
}
}
总结:
- 计数排序只能用于整数排序
- 计数排序在数据范围集中时的效率高,应用场景有限
- 时间复杂度:O(N~range)
- 空间复杂度:O(range)
- 稳定性:稳定
我们下期见。
边栏推荐
- 李宏毅《机器学习》丨4. Deep Learning(深度学习)
- Pop up layer prompt in the background
- 在abaqus中使用PyQt设计GUI
- Geodetic coordinate system to Martian coordinate system
- Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
- 优化Hypermesh脚本性能的几点建议
- JS queue
- Numpy ndarray learning < I > Foundation
- 关于web对接针式打印机问题,Lodop使用
- HyperMesh运行脚本文件的几种方法
猜你喜欢

Instructions for mictr01 Tester development kit (vibrating wire acquisition reader)

Detectron2 installation and testing

LabVIEW LINX Toolkit控制Arduino设备(拓展篇—1)

2021 Yahong pen test questions

IT远程运维是什么意思?远程运维软件哪个好?

c语言编程当中两个!!的作用

Laser rangefinder non-contact surface crack monitor

Stm32cube infrared remote control: input capture

Ask if you don't understand, and quickly become an advanced player of container service!

Zhengda cup hacker marathon data analysis competition
随机推荐
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Brief tutorial for soft exam system architecture designer | software debugging
High precision absolute angle sensor application high speed angle monitoring
Common problems and precautions of remote serial port server (adapter) uart/i2c/1-wire/spi PS304
STM32F103C8T6 + 0.96“ I2C OLED显示3D_Cube
PHP about problems such as memory overflow timeout caused by large amount of data exporting or traversing data
Automatically pack compressed backup download and delete bat script commands
QT QString详解
flashfxp 530 User cannot log in. ftp
Qt学习之Qt Designer(设计师)
2021-04-02
JS stack
Numpy ndarray learning < I > Foundation
Use py to automatically generate weekly reports based on log records
Two of C language programming!! Role of
ANSA二次开发 - Apps和ANSA插件管理
QT打包
R language uses ggpairs function of ggally package to visualize pairwise relationship graph of grouping multivariable, set alpha parameter to change image transparency, diagonal continuous variable de
Zhengda cup hacker marathon data analysis competition