当前位置:网站首页>计数排序基础思路
计数排序基础思路
2022-07-06 22:04:00 【就要 宅在家】
所谓计数排序,也可以称为散列表 。也是一种简单的哈希桶。
今天,小编带大家来了解计数排序的基本思路。
一。基本思路
以升序为例,计数排序通俗来讲,分为三个步骤。
首先制作包含所有要排序的数的桶(相同的数制作一个桶即可)。
以2,3,6,1,4,1,2,3,7,6,8,9,5,4,3举例,就是制作9个桶,分别代表1,2,3,4,5,6,7,8,9。
第二步, 把所有的数依次放入桶中,桶中的数字代表该数有多少个。
第三步,从小到大依次把桶中的数全部拿出来。
排序完成。
小编希望大家自主实现一下代码,难度不大,相信自己!
ps:桶可以用数组下标代替。
二。代码实现
void CountSort(int* a, int n)
{
if (n <= 0) return;
assert(a);
int min = a[0], max = a[0];
for (int i = 0; i < n; i++)//找出排序范围,减少空间浪费
{
if (min > a[i]) min = a[i];
if (max < a[i]) max = a[i];
}
int* tmp = (int*)new int[max - min + 1], range = max - min + 1;
memset(tmp, 0, sizeof(int) * range);
for (int i = 0; i < n; i++)//入桶
{
tmp[a[i] - min]++;
}
int sub = 0;
for (int i = 0; i < range; i++)//出桶
{
while (tmp[i]--)
{
a[sub++] = i + min;
}
}
}
三。问题
时间复杂度:O(n)
空间复杂度:O(n)
计数排序的作用范围狭小,浮点数、结构体之类都不适用,且范围越大空间复杂度和空间浪费就越明显。
三连支持一下吧 如有错误,敬请斧正
边栏推荐
- 用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30
- Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
- Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
- Both primary and secondary equipment numbers are 0
- Win11截图键无法使用怎么办?Win11截图键无法使用的解决方法
- 两个div在同一行,两个div不换行「建议收藏」
- 各路行业大佬称赞的跨架构开发“神器”,你get同款了吗?
- Nanopineo use development process record
- UltraEdit-32 warm prompt: right association, cancel bak file [easy to understand]
- 论文上岸攻略 | 如何快速入门学术论文写作
猜你喜欢
CUDA Programming
SSM+JSP实现企业管理系统(OA管理系统源码+数据库+文档+PPT)
Ssm+jsp realizes enterprise management system (OA management system source code + database + document +ppt)
Digital chemical plants realize the coexistence of advantages of high quality, low cost and fast efficiency
NTU notes 6422quiz review (1-3 sections)
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
EasyCVR无法使用WebRTC进行播放,该如何解决?
Optimization of channel status offline of other server devices caused by easycvr cluster restart
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
Video fusion cloud platform easycvr video Plaza left column list style optimization
随机推荐
赠票速抢|行业大咖纵论软件的质量与效能 QECon大会来啦
Mongo shell, the most complete mongodb in history
NFT meta universe chain diversified ecosystem development case
MySQL forgot how to change the password
NTU notes 6422quiz review (1-3 sections)
DAB-DETR: DYNAMIC ANCHOR BOXES ARE BETTER QUERIES FOR DETR翻译
How to write a resume that shines in front of another interviewer [easy to understand]
ESG Global Leaders Summit | Intel Wang Rui: coping with global climate challenges with the power of science and technology
Have you got the same "artifact" of cross architecture development praised by various industry leaders?
Zero knowledge private application platform aleo (1) what is aleo
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
EasyCVR视频广场点击播放时,主菜单高亮效果消失问题的修复
Intel and Xinbu technology jointly build a machine vision development kit to jointly promote the transformation of industrial intelligence
JS form get form & get form elements
【实践出真理】import和require的引入方式真的和网上说的一样吗
[knife-4j quickly build swagger]
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
POJ training plan 2253_ Frogger (shortest /floyd)