当前位置:网站首页>排序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)
- 稳定性:稳定
我们下期见。
边栏推荐
猜你喜欢

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

Laser rangefinder non-contact surface crack monitor

laravel

Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi

Installation points and precautions of split angle probe

QT打包

1. Simple command line connection to database

不懂就问,快速成为容器服务进阶玩家!

ANSYS二次开发 - MFC界面调用ADPL文件

The video Number finds the golden key, and Tiktok imitates the latecomers
随机推荐
2021-10-21 notes
魏建军骑宝马也追不上李书福
Automatically pack compressed backup download and delete bat script commands
解决uniapp等富文本图片宽度溢出
Ask if you don't understand, and quickly become an advanced player of container service!
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
2021-04-02
Qt学习之安装
Advantages of optical rain gauge over tipping bucket rain gauge
关于web对接针式打印机问题,Lodop使用
I can only sell the company after the capital has been "cut off" for two years
Laser rangefinder non-contact surface crack monitor
小程序中的分页查询
el-input限制只能输入规定的数
The deep displacement monitoring system wk813 is used to measure the deep displacement of slopes, dams, embankments, railways and building foundation pit excavation
Pop up layer prompt in the background
动态规划 --- 数位统计DP
Why do most people who learn programming go to Shenzhen and Beijing?
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书