Both counting sort and radix sort are special methods of bucket sort
It's been really cold these two nights...
So, what kind of sorting is counting sort?
Counting sort is not a comparative sorting algorithm, which was proposed by Harold H. Seward in 1954, and the time complexity is reduced to 100% by counting O(N)
.
What are the steps of the counting sort algorithm?
- Step 1: Find the element with the largest value in the original array, denoted as
max
. - Step 2 : Create a new array
count
whose length ismax
increased by 1, and the default value of its elements is 0. - Step 3 : Traverse the elements in the original array, take the elements in the original array as
count
the index of the array, and take the number of occurrences of the elements in the original array ascount
the element value of the array. - Step 4 : Create the result array
result
, starting indexindex
. - Step 5 : Traverse
count
the array, find the element whose element value is greater than 0, and fill the corresponding index into theresult
array as the element value. Each time it is processed,count
the value of the element in it is reduced by 1 until the element value is not greater than 0. , processingcount
the remaining elements in turn. - Step 6 : Return the result array
result
.
This is very abstract. Is there a way of speaking that is easy to understand, such as diagrams?
But it's a waste of space!
For example, a set of data {101,109,108,102,110,107,103}
, where the maximum value is 110, according to the previous idea, we need to create a count array with a length of 111, but we can find that [0,100]
the space in front of it is completely wasted.
How to optimize it?
The length of the array is determined as max-min+1
not only the maximum value, but also the minimum value should be found, and the length of the count array should be determined according to the difference between the two.
Let's take it easy
Next, we will use the optimized ideas to solve a hand-tear sorting problem that is often tested in interviews.
topic
Submission
count sort code (preferred)
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 radix sort 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 big guy's article to understand the counting sorting algorithm! - Programmer Ogawa- Blog Park