Common solutions for common algorithm interview questions-1 counting sort

Source: Internet
Author: User

AlgorithmOne of the frequently asked questions is searching or sorting. I personally feel that 80% of the questions are related to searching and sorting.

The time complexity of most common sorting algorithms is O (nlogn)

This can only be a general solution.

This requires a low level of time complexity in algorithm interview questions,

For example

It is known that the length of an array is m and the value range is 1-M. The elements in the middle may be repeated or not repeated.

How can I find out which of the (1-m) numbers are repeated and which do not appear in the O (m) case?

The essence of counting sort is to create an array with a length of M. An array subscript represents a number, and the value in the array represents the number of times this element appears (the initial value is 0)

Then, when m is traversed at a time, A number is added to the corresponding subscript by 1.

In the end, we can obtain the number of occurrences of an containing all elements.

Expand it to obtain the sorted array.

This is a special algorithm that can only solve special problems, but its time complexity is O (n)

If you encounter sorting or searching algorithms, try counting sort first.

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: info-contact@alibabacloud.com 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.