当前位置:网站首页>计数排序基础思路
计数排序基础思路
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)
计数排序的作用范围狭小,浮点数、结构体之类都不适用,且范围越大空间复杂度和空间浪费就越明显。
三连支持一下吧 如有错误,敬请斧正
边栏推荐
- Win11截图键无法使用怎么办?Win11截图键无法使用的解决方法
- Pyqt5 out of focus monitoring no operation timer
- The most complete security certification of mongodb in history
- 科兴与香港大学临床试验中心研究团队和香港港怡医院合作,在中国香港启动奥密克戎特异性灭活疫苗加强剂临床试验
- EasyCVR无法使用WebRTC进行播放,该如何解决?
- Win11控制面板快捷键 Win11打开控制面板的多种方法
- 日常工作中程序员最讨厌哪些工作事项?
- 超越Postman,新一代国产调试工具Apifox,用起来够优雅
- Easycvr cannot be played using webrtc. How to solve it?
- Zero knowledge private application platform aleo (1) what is aleo
猜你喜欢

Practice Guide for interface automation testing (middle): what are the interface testing scenarios

See Gardenia minor

Win11 control panel shortcut key win11 multiple methods to open the control panel
![[record of question brushing] 2 Add two numbers](/img/3b/f8fec79dc2b5088ac57f8d7a18aea6.png)
[record of question brushing] 2 Add two numbers

On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
![[team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp](/img/eb/9aed3bbbd5b6ec044ec5542297f3c6.jpg)
[team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp

深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚

英特尔David Tuhy:英特尔傲腾技术成功的原因

AI 落地新题型 RPA + AI =?

The most complete security certification of mongodb in history
随机推荐
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
jvm是什么?jvm调优有哪些目的?
[on automation experience] the growth path of automated testing
Ssm+jsp realizes enterprise management system (OA management system source code + database + document +ppt)
Video fusion cloud platform easycvr video Plaza left column list style optimization
Common methods of list and map
POJ training plan 2253_ Frogger (shortest /floyd)
[multi threading exercise] write a multi threading example of the producer consumer model.
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
Practice Guide for interface automation testing (middle): what are the interface testing scenarios
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
機器人(自動化)課程的持續學習-2022-
【实践出真理】import和require的引入方式真的和网上说的一样吗
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
英特尔David Tuhy:英特尔傲腾技术成功的原因
【自动化经验谈】自动化测试成长之路
别样肉客联手德克士在全国部分门店推出别样汉堡
[knife-4j quickly build swagger]
ESG全球领导者峰会|英特尔王锐:以科技之力应对全球气候挑战
Why does WordPress open so slowly?