当前位置:网站首页>计数排序基础思路
计数排序基础思路
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)
计数排序的作用范围狭小,浮点数、结构体之类都不适用,且范围越大空间复杂度和空间浪费就越明显。
三连支持一下吧 如有错误,敬请斧正
边栏推荐
- [system management] clear the icon cache of deleted programs in the taskbar
- Use facet to record operation log
- DAB-DETR: DYNAMIC ANCHOR BOXES ARE BETTER QUERIES FOR DETR翻译
- The request request is encapsulated in uni app, which is easy to understand
- Five years of automated testing, and finally into the ByteDance, the annual salary of 30W is not out of reach
- EasyUI export excel cannot download the method that the box pops up
- [record of question brushing] 2 Add two numbers
- 《原动力 x 云原生正发声 降本增效大讲堂》第三讲——Kubernetes 集群利用率提升实践
- 一度辍学的数学差生,获得今年菲尔兹奖
- [on automation experience] the growth path of automated testing
猜你喜欢
![[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis](/img/82/8f5b6f388d5676cb7ff902ba80d9d2.jpg)
[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis

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

The request request is encapsulated in uni app, which is easy to understand

超越Postman,新一代国产调试工具Apifox,用起来够优雅

【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ

Easycvr cannot be played using webrtc. How to solve it?

Digital chemical plants realize the coexistence of advantages of high quality, low cost and fast efficiency

程序员上班摸鱼,这么玩才高端!
![[multi threading exercise] write a multi threading example of the producer consumer model.](/img/8a/4a2836f905968f42e0ef339d900b19.jpg)
[multi threading exercise] write a multi threading example of the producer consumer model.

Digital chemical plant management system based on Virtual Simulation Technology
随机推荐
Surpassing postman, the new generation of domestic debugging tool apifox is elegant enough to use
1.19.11. SQL client, start SQL client, execute SQL query, environment configuration file, restart policy, user-defined functions, constructor parameters
Easycvr cannot be played using webrtc. How to solve it?
Kotlin Compose Text支持两种颜色
【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
Optimization of channel status offline of other server devices caused by easycvr cluster restart
Data security -- 12 -- Analysis of privacy protection
See Gardenia minor
高薪程序员&面试题精讲系列120之Redis集群原理你熟悉吗?如何保证Redis的高可用(上)?
Practice Guide for interface automation testing (middle): what are the interface testing scenarios
The easycvr platform is connected to the RTMP protocol, and the interface call prompts how to solve the error of obtaining video recording?
未婚夫捐5亿美元给女PI,让她不用申请项目,招150位科学家,安心做科研!
一图看懂!为什么学校教了你Coding但还是不会的原因...
NFT meta universe chain diversified ecosystem development case
VIM - own active button indent this command "suggestions collection"
kivy教程之设置窗体大小和背景(教程含源码)
2022 electrician cup question B analysis of emergency materials distribution under 5g network environment
Mathematical analysis_ Notes_ Chapter 10: integral with parameters
英特尔与信步科技共同打造机器视觉开发套件,协力推动工业智能化转型