当前位置:网站首页>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
边栏推荐
- A picture to understand! Why did the school teach you coding but still not
- Jetson nano配置pytorch深度学习环境//待完善
- 抖音或将推出独立种草社区平台:会不会成为第二个小红书
- Why does WordPress open so slowly?
- Digital chemical plants realize the coexistence of advantages of high quality, low cost and fast efficiency
- How to solve the problem of adding RTSP device to easycvr cluster version and prompting server ID error?
- Complimentary tickets quick grab | industry bigwigs talk about the quality and efficiency of software qecon conference is coming
- Master the secrets of software security testing methods, and pinch the security test report with your hands
- [team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp
- C # use Siemens S7 protocol to read and write PLC DB block
猜你喜欢
A detailed explanation of head pose estimation [collect good articles]
1.19.11. SQL client, start SQL client, execute SQL query, environment configuration file, restart policy, user-defined functions, constructor parameters
Video fusion cloud platform easycvr video Plaza left column list style optimization
EasyCVR集群重启导致其他服务器设备通道状态离线情况的优化
Win11截图键无法使用怎么办?Win11截图键无法使用的解决方法
用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30
See Gardenia minor
In cooperation with the research team of the clinical trial center of the University of Hong Kong and Hong Kong Gangyi hospital, Kexing launched the clinical trial of Omicron specific inactivated vacc
数学分析_笔记_第10章:含参变量积分
[written to the person who first published the paper] common problems in writing comprehensive scientific and Technological Papers
随机推荐
计数排序基础思路
一图看懂!为什么学校教了你Coding但还是不会的原因...
namespace基础介绍
Why does WordPress open so slowly?
SQL where multiple field filtering
[on automation experience] the growth path of automated testing
什么是Web3
Camera calibration (I): robot hand eye calibration
视频融合云平台EasyCVR视频广场左侧栏列表样式优化
抖音或将推出独立种草社区平台:会不会成为第二个小红书
buildroot的根文件系统提示“depmod:applt not found”
各路行业大佬称赞的跨架构开发“神器”,你get同款了吗?
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
图灵诞辰110周年,智能机器预言成真了吗?
《原动力 x 云原生正发声 降本增效大讲堂》第三讲——Kubernetes 集群利用率提升实践
Different meat customers joined hands with Dexter to launch different hamburgers in some stores across the country
[OA] excel document generator: openpyxl module
Complimentary tickets quick grab | industry bigwigs talk about the quality and efficiency of software qecon conference is coming
System framework of PureMVC
Win11玩绝地求生(PUBG)崩溃怎么办?Win11玩绝地求生崩溃解决方法