当前位置:网站首页>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
边栏推荐
- JetBrain Pycharm的一系列快捷键
- Zero knowledge private application platform aleo (1) what is aleo
- [on automation experience] the growth path of automated testing
- Formation continue en robotique (automatisation) - 2022 -
- leetcode 53. Maximum Subarray 最大子数组和(中等)
- Win11玩绝地求生(PUBG)崩溃怎么办?Win11玩绝地求生崩溃解决方法
- How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
- sscanf,sscanf_s及其相关使用方法「建议收藏」
- sscanf,sscanf_ S and its related usage "suggested collection"
- Why does WordPress open so slowly?
猜你喜欢

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

Optimization of channel status offline of other server devices caused by easycvr cluster restart
![[coded font series] opendyslexic font](/img/5e/e1512ffe494b5d0e7d6d6765644126.png)
[coded font series] opendyslexic font

Five years of automated testing, and finally into the ByteDance, the annual salary of 30W is not out of reach
![[team learning] [34 issues] scratch (Level 2)](/img/29/8383d753eedcffd874bc3f0194152a.jpg)
[team learning] [34 issues] scratch (Level 2)

How do test / development programmers get promoted? From nothing, from thin to thick

NTU notes 6422quiz review (1-3 sections)

AI 落地新题型 RPA + AI =?

What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash

九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
随机推荐
[knife-4j quickly build swagger]
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
英特尔David Tuhy:英特尔傲腾技术成功的原因
Intel and Xinbu technology jointly build a machine vision development kit to jointly promote the transformation of industrial intelligence
[coded font series] opendyslexic font
What is CGI, IIS, and VPS "suggested collection"
A picture to understand! Why did the school teach you coding but still not
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
Formation continue en robotique (automatisation) - 2022 -
组织实战攻防演练的5个阶段
架构实战训练营|课后作业|模块 6
Digital chemical plant management system based on Virtual Simulation Technology
[team learning] [34 sessions] Alibaba cloud Tianchi online programming training camp
Easycvr cannot be played using webrtc. How to solve it?
True global ventures' newly established $146million follow-up fund was closed, of which the general partner subscribed $62million to invest in Web3 winners in the later stage
Win11截图键无法使用怎么办?Win11截图键无法使用的解决方法
Have you got the same "artifact" of cross architecture development praised by various industry leaders?
Zero knowledge private application platform aleo (1) what is aleo
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
MySQL null value processing and value replacement