当前位置:网站首页>计数排序基础思路
计数排序基础思路
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)
计数排序的作用范围狭小,浮点数、结构体之类都不适用,且范围越大空间复杂度和空间浪费就越明显。
三连支持一下吧 如有错误,敬请斧正
边栏推荐
- Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
- Five years of automated testing, and finally into the ByteDance, the annual salary of 30W is not out of reach
- NTU notes 6422quiz review (1-3 sections)
- This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
- What is CGI, IIS, and VPS "suggested collection"
- 论文上岸攻略 | 如何快速入门学术论文写作
- 测试/开发程序员怎么升职?从无到有,从薄变厚.......
- Digital chemical plant management system based on Virtual Simulation Technology
- Implementation of JSTL custom function library
- Hangzhou Electric 3711 binary number
猜你喜欢
DAB-DETR: DYNAMIC ANCHOR BOXES ARE BETTER QUERIES FOR DETR翻译
The root file system of buildreoot prompts "depmod:applt not found"
Digital chemical plants realize the coexistence of advantages of high quality, low cost and fast efficiency
Dab-detr: dynamic anchor boxes are better queries for Detr translation
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
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
[team learning] [34 sessions] Alibaba cloud Tianchi online programming training camp
Intel and Xinbu technology jointly build a machine vision development kit to jointly promote the transformation of industrial intelligence
Mongo shell, the most complete mongodb in history
程序员上班摸鱼,这么玩才高端!
随机推荐
2022 middle school Youth Cup mathematical modeling question B fertility policy research ideas under the background of open three children
MySQL split method usage
Implementation of JSTL custom function library
用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30
广告归因:买量如何做价值衡量?
Kotlin Compose Text支持两种颜色
leetcode 53. Maximum Subarray 最大子数组和(中等)
Imitate Tengu eating the moon with Avatar
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
案例大赏:英特尔携众多合作伙伴推动多领域AI产业创新发展
Pyqt5 out of focus monitoring no operation timer
[written to the person who first published the paper] common problems in writing comprehensive scientific and Technological Papers
The easycvr platform is connected to the RTMP protocol, and the interface call prompts how to solve the error of obtaining video recording?
C # use Siemens S7 protocol to read and write PLC DB block
SSM+jsp实现仓库管理系统,界面那叫一个优雅
leetcode 53. Maximum subarray maximum subarray sum (medium)
EasyCVR集群版本添加RTSP设备提示服务器ID错误,该如何解决?
AI landing new question type RPA + AI =?
MySQL null value processing and value replacement
2022 electrician cup a question high proportion wind power system energy storage operation and configuration analysis ideas