当前位置:网站首页>Basic idea of counting and sorting
Basic idea of counting and sorting
2022-07-07 04:38:00 【Just stay at home】
The so-called counting sort , Also known as hash table . It is also a simple hash bucket .
today , Xiaobian takes you to understand the basic idea of counting and sorting .
One . The basic idea
Take ascending order , Generally speaking, counting and sorting , There are three steps .
First make a bucket containing all the numbers to be sorted ( Make a bucket with the same number ).
With 2,3,6,1,4,1,2,3,7,6,8,9,5,4,3 give an example , It's making 9 A barrel , Represent the 1,2,3,4,5,6,7,8,9.
The second step , Put all the numbers into the bucket in turn , The number in the bucket represents the number .
The third step , Take out all the numbers in the bucket from small to large .
Sort complete .
Xiaobian hopes that you can independently implement the code , Difficulty is not great , Believe in yourself !
ps: Buckets can be replaced by array subscripts .
Two . Code implementation
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++)// Find the sorting range , Reduce space waste
{
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++)// Into the barrel
{
tmp[a[i] - min]++;
}
int sub = 0;
for (int i = 0; i < range; i++)// Out of the barrel
{
while (tmp[i]--)
{
a[sub++] = i + min;
}
}
}
3、 ... and . problem
Time complexity :O(n)
Spatial complexity :O(n)
The scope of counting and sorting is narrow , Floating point numbers 、 Structures and the like are not applicable , And the larger the scope, the more obvious the space complexity and space waste .
Let's support San Lian If there is a mistake , Please correct
边栏推荐
- Kotlin compose text supports two colors
- NanopiNEO使用开发过程记录
- 架构实战训练营|课后作业|模块 6
- ESG Global Leaders Summit | Intel Wang Rui: coping with global climate challenges with the power of science and technology
- See Gardenia minor
- Win11 control panel shortcut key win11 multiple methods to open the control panel
- 视频融合云平台EasyCVR视频广场左侧栏列表样式优化
- Camera calibration (I): robot hand eye calibration
- [on automation experience] the growth path of automated testing
- 每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
猜你喜欢
機器人(自動化)課程的持續學習-2022-
Oracle -- 视图与序列
Win11玩绝地求生(PUBG)崩溃怎么办?Win11玩绝地求生崩溃解决方法
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
[on automation experience] the growth path of automated testing
Intel David tuhy: the reason for the success of Intel aoten Technology
Optimization of channel status offline of other server devices caused by easycvr cluster restart
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
The root file system of buildreoot prompts "depmod:applt not found"
数学分析_笔记_第10章:含参变量积分
随机推荐
Network Security Learning - Information Collection
[team learning] [34 sessions] Alibaba cloud Tianchi online programming training camp
Jetson nano配置pytorch深度学习环境//待完善
Win11图片打不开怎么办?Win11无法打开图片的修复方法
acwing 843. n-皇后问题
EasyCVR无法使用WebRTC进行播放,该如何解决?
程序员上班摸鱼,这么玩才高端!
这项15年前的「超前」技术设计,让CPU在AI推理中大放光彩
This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
How to solve the problem of adding RTSP device to easycvr cluster version and prompting server ID error?
A detailed explanation of head pose estimation [collect good articles]
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
Detect when a tab bar item is pressed
How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
Poor math students who once dropped out of school won the fields award this year
How do test / development programmers get promoted? From nothing, from thin to thick
Easycvr cannot be played using webrtc. How to solve it?
接口自动化测试实践指导(中):接口测试场景有哪些
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报