当前位置:网站首页>O (n) complexity hand tear sorting interview questions | an article will help you understand counting sorting
O (n) complexity hand tear sorting interview questions | an article will help you understand counting sorting
2022-06-24 22:40:00 【Rice shrimp】
Count sorting And Radix sorting It is a special method of bucket sorting
It's really cold these two nights ...
that , What sort of sort is count sort ?
Counting sort is not a comparison sort algorithm , The algorithm is based on 1954 Year by year Harold H. Seward Put forward , The time complexity is reduced to O(N)
.
What are the steps of the counting sorting algorithm ?
- First step : Find out the largest element in the original array , Write it down as
max
. - The second step : Create a new array
count
, Its length ismax
Add 1, The default values of its elements are 0. - The third step : Go through the elements in the original array , Take the elements in the original array as
count
Index of array , Take the number of elements in the original array ascount
The element value of the array . - Step four : Create an array of results
result
, Starting indexindex
. - Step five : Traverse
count
Array , Find out that the element value is greater than 0 The elements of , Fill its corresponding index as the element value toresult
Go to... In the array , Every time I deal with ,count
The value of this element in minus 1, Until the value of the element is not greater than 0, Sequential processingcount
The rest of the elements in . - Step six : Return result array
result
.
It's abstract , Is there an easy way to understand , For example, illustration ?
But it's a waste of space !
Like a set of data {101,109,108,102,110,107,103}
, The maximum value is 110, Follow the previous thinking , We need to create a length of 111 Count array of , But we can see , The one in front of it [0,100]
That's a complete waste of space .
So how to optimize ?
Set the length of the array to max-min+1
, That is to find out not only the maximum value , And find the minimum , Determine the length of the count array based on the difference between the two .
Let's make a joke and relax
Next, we will use the optimized idea to solve a common hand tearing sorting problem in the interview
Topic
Submission
Count sort code ( optimization )
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
int n = nums.size();
int max = nums[0], min = nums[0];
for(int i=1; i<n; i++) {
if(nums[i] > max) max = nums[i];
if(nums[i] < min) min = nums[i];
}
vector<int> count(max-min+1,0);
for(int i=0; i<n; i++)
count[nums[i]-min]++;
int k=0;
for(int i=0; i<max-min+1; i++) {
while(count[i]--) {
nums[k++] = i+min;
}
}
return nums;
}
};
Corresponding cardinality sorting code
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
int n = nums.size();
int max = abs(nums[0]);
for(int i=1; i<n; i++)
if(max<abs(nums[i]))
max = abs(nums[i]);
int w = 0;
while(max>0) {
max /= 10;
w++;
}
int flag = 1;
vector<int> ans(n);
for(int i=0; i<w; i++) {
vector<int> bucket(19,0);
for(int j=0; j<n; j++) {
int temp = nums[j]/flag%10+9;
bucket[temp]++;
}
for(int j=1; j<19; j++)
bucket[j] += bucket[j-1];
for(int j=n-1; j>=0; j--) {
int temp = nums[j]/flag%10+9;
ans[--bucket[temp]] = nums[j];
}
nums.swap(ans);
flag*=10;
}
return nums;
}
};
Refer to the boss's A paper to understand the counting and sorting algorithm ! - Programmer Ogawa - Blog Garden
边栏推荐
- Problèmes de concurrence dans l'allocation de mémoire en tas
- Concurrency of heap memory allocation
- NIO、BIO、AIO
- Programmers become gods by digging holes in one year, carrying flags in five years and becoming gods in ten years
- Docker 安装 Redis-5.0.12,详细步骤
- 为什么有的程序员能力一般却能拿到好offer?
- 软件设计的七大原则
- 零代码即可将数据可视化应用到企业管理中
- 【軟件工程】期末重點
- 无心剑汉英双语诗003. 《书海》
猜你喜欢
FANUC机器人_KAREL编程入门学习(1)
nuScenes——数据集配置过程中遇到图像文件缺失或大小为0时的补救方法
面试害怕被问MySQL相关问题 ?这份三万字精华总结 + 面试100 问,吊打面试官完全够了
Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020
Genesis公链与美国一众加密投资者齐聚Consensus 2022
Future development of education industry of e-commerce Express
NiO zero copy
Fanuc robot_ Introduction to Karel programming (1)
AQS源码分析
树莓派初步使用
随机推荐
HTTP的缓存控制
Unable to use the bean introduced into the jar package
树莓派初步使用
Extend your kubernetes API with aggregated apiserver
Structure du disque
Industrial development status of virtual human
NiO, bio, AIO
无心剑汉英双语诗003. 《书海》
Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020
ThreadLocal内存泄漏问题
Envoy obtain the real IP address of the client
Development of live broadcast software app, and automatic left-right sliding rotation chart advertising
Interrupt, interrupted, isinterrupted differences
socket done
理想L9,智能座舱新潮流
Chapter 10 project stakeholder management
[personal experiment report]
零代码即可将数据可视化应用到企业管理中
1. fully explain the basic principles of IPSec
CSRF and SSRF for web attacks