Sorting algorithm--bucket sort

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.