当前位置:网站首页>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
边栏推荐
- mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
- NanopiNEO使用开发过程记录
- 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
- namespace基础介绍
- Win11控制面板快捷键 Win11打开控制面板的多种方法
- 图灵诞辰110周年,智能机器预言成真了吗?
- How do test / development programmers get promoted? From nothing, from thin to thick
- VM virtual machine operating system not found and NTLDR is missing
- Two divs are on the same line, and the two divs do not wrap "recommended collection"
- 微信能开小号了,拼多多“砍一刀”被判侵权,字节VR设备出货量全球第二,今日更多大新闻在此
猜你喜欢

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

Vscode 如何使用内置浏览器?

namespace基础介绍

Video fusion cloud platform easycvr video Plaza left column list style optimization

Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices

Have you got the same "artifact" of cross architecture development praised by various industry leaders?

Surpassing postman, the new generation of domestic debugging tool apifox is elegant enough to use
![[coded font series] opendyslexic font](/img/5e/e1512ffe494b5d0e7d6d6765644126.png)
[coded font series] opendyslexic font

What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures

Optimization of channel status offline of other server devices caused by easycvr cluster restart
随机推荐
ESG全球领导者峰会|英特尔王锐:以科技之力应对全球气候挑战
Acl2022 | decomposed meta learning small sample named entity recognition
The root file system of buildreoot prompts "depmod:applt not found"
Kivy tutorial of setting the size and background of the form (tutorial includes source code)
Network Security Learning - Information Collection
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
Wechat can play the trumpet. Pinduoduo was found guilty of infringement. The shipment of byte VR equipment ranks second in the world. Today, more big news is here
A series of shortcut keys for jetbrain pychar
接口自动化测试实践指导(中):接口测试场景有哪些
Complimentary tickets quick grab | industry bigwigs talk about the quality and efficiency of software qecon conference is coming
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
主设备号和次设备号均为0
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
[written to the person who first published the paper] common problems in writing comprehensive scientific and Technological Papers
Oracle -- 视图与序列
Lessons and thoughts of the first SQL injection
数学分析_笔记_第10章:含参变量积分
acwing 843. n-皇后问题
Easycvr cannot be played using webrtc. How to solve it?
浙江大学周亚金:“又破又立”的顶尖安全学者,好奇心驱动的行动派