当前位置:网站首页>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
边栏推荐
- leetcode 53. Maximum subarray maximum subarray sum (medium)
- 日常工作中程序员最讨厌哪些工作事项?
- Ssm+jsp realizes enterprise management system (OA management system source code + database + document +ppt)
- 数学分析_笔记_第10章:含参变量积分
- Dab-detr: dynamic anchor boxes are better queries for Detr translation
- What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
- acwing 843. n-皇后问题
- Video fusion cloud platform easycvr video Plaza left column list style optimization
- [ArcGIS tutorial] thematic map production - population density distribution map - population density analysis
- How to conduct website testing of software testing? Test strategy let's go!
猜你喜欢
[multi threading exercise] write a multi threading example of the producer consumer model.
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
Easycvr cannot be played using webrtc. How to solve it?
[team learning] [34 sessions] Alibaba cloud Tianchi online programming training camp
Ssm+jsp realizes the warehouse management system, and the interface is called an elegant interface
EasyCVR视频广场点击播放时,主菜单高亮效果消失问题的修复
[coded font series] opendyslexic font
Practice Guide for interface automation testing (middle): what are the interface testing scenarios
How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
Kivy tutorial of setting the size and background of the form (tutorial includes source code)
随机推荐
Jetson nano配置pytorch深度学习环境//待完善
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
视频融合云平台EasyCVR视频广场左侧栏列表样式优化
See Gardenia minor
[knife-4j quickly build swagger]
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
英特尔David Tuhy:英特尔傲腾技术成功的原因
什么是Web3
leetcode 53. Maximum subarray maximum subarray sum (medium)
A series of shortcut keys for jetbrain pychar
Video fusion cloud platform easycvr video Plaza left column list style optimization
Intel and Xinbu technology jointly build a machine vision development kit to jointly promote the transformation of industrial intelligence
Five years of automated testing, and finally into the ByteDance, the annual salary of 30W is not out of reach
Golang compresses and decompresses zip files
What is CGI, IIS, and VPS "suggested collection"
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
计数排序基础思路
Zero knowledge private application platform aleo (1) what is aleo
How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures