The basic idea of bucket sequencing
Now there is an array a, this array has a size element, the range of elements is 0~max. This size data is required to be sorted.
Create an array of size max+1 B, each element is 0. To traverse a from the beginning, when traversing to a[i], the value of b[a[i]] is added 1;
When the entire sweep of a is finished, output B gets the final sort result.
A bucket sort of face question
Interviewer: Please implement a sorting algorithm that requires a time complexity of O (n).
Titles: What numbers to sort ah, how many numbers?
Interviewer: To the age of all employees in the company, our company has a total of tens of thousands of employees.
Candidate: That means the numbers are in a smaller range, right?
Interviewer: Yes.
Candidate: Can I use a secondary space?
Interviewer: Only the constant size of the secondary space is allowed and no O (n) is found.
Using the same sort of code implementation
Sorting algorithm--bucket sort