当前位置:网站首页>计数排序基础思路
计数排序基础思路
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)
计数排序的作用范围狭小,浮点数、结构体之类都不适用,且范围越大空间复杂度和空间浪费就越明显。
三连支持一下吧 如有错误,敬请斧正
边栏推荐
- 两个div在同一行,两个div不换行「建议收藏」
- The most complete security certification of mongodb in history
- The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
- GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
- 1.19.11. SQL client, start SQL client, execute SQL query, environment configuration file, restart policy, user-defined functions, constructor parameters
- AI 落地新题型 RPA + AI =?
- Win11远程桌面连接怎么打开?Win11远程桌面连接的五种方法
- NFT meta universe chain diversified ecosystem development case
- Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
- [OA] excel document generator: openpyxl module
猜你喜欢
In cooperation with the research team of the clinical trial center of the University of Hong Kong and Hong Kong Gangyi hospital, Kexing launched the clinical trial of Omicron specific inactivated vacc
How do test / development programmers get promoted? From nothing, from thin to thick
Why does WordPress open so slowly?
The most complete deployment of mongodb in history
Intel David tuhy: the reason for the success of Intel aoten Technology
图灵诞辰110周年,智能机器预言成真了吗?
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
Network Security Learning - Information Collection
Surpassing postman, the new generation of domestic debugging tool apifox is elegant enough to use
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
随机推荐
The first introduction of the most complete mongodb in history
Win11 control panel shortcut key win11 multiple methods to open the control panel
[system management] clear the icon cache of deleted programs in the taskbar
《原动力 x 云原生正发声 降本增效大讲堂》第三讲——Kubernetes 集群利用率提升实践
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
Surpassing postman, the new generation of domestic debugging tool apifox is elegant enough to use
Win11图片打不开怎么办?Win11无法打开图片的修复方法
SSM+jsp实现仓库管理系统,界面那叫一个优雅
[team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp
軟件測試之網站測試如何進行?測試小攻略走起!
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
Unity3d can change colors and display samples in a building GL material
AI 落地新题型 RPA + AI =?
True Global Ventures新成立的1.46亿美元后续基金关账,其中普通合伙人认缴6,200万美元以对后期阶段的Web3赢家进行投资
科兴与香港大学临床试验中心研究团队和香港港怡医院合作,在中国香港启动奥密克戎特异性灭活疫苗加强剂临床试验
jvm是什么?jvm调优有哪些目的?
图灵诞辰110周年,智能机器预言成真了吗?
案例大赏:英特尔携众多合作伙伴推动多领域AI产业创新发展
Win11截图键无法使用怎么办?Win11截图键无法使用的解决方法
EasyCVR无法使用WebRTC进行播放,该如何解决?