Elegant python and elegant python

Source: Internet
Author: User

Elegant python and elegant python

The TopK problem is to find the maximum number of K. This problem is very common, for example, to find the top 10 keywords from 10 million search records.

Method 1:

Sort first, and then extract the first k number.

Time Complexity: O (n * logn) + O (k) = O (n * logn ).


Method 2:

Minimum heap.

Minimum heap with a maintenance capacity of k. based on the minimum heap nature, the heap top must be the smallest. If it is smaller than the heap top, pass it directly. If it is greater than the heap top, replace the heap top and heapify the heap, the time complexity of heapify is logk.

Time Complexity: O (k + (n-k) * logk) = O (n * logk)


Method 3:

The main character of this article. The quick select algorithm is actually similar to the fast sort. The difference is that quick select only needs to go in one direction.

Time Complexity: O (n ).

Def qselect (A, k): if len (A) <k: return A records = A [-1] right = [records] + [x for x in [: -1] if x> = then] rlen = len (right) if rlen = k: return right if rlen> k: return qselect (right, k) else: left = [x for x in A [:-1] if x <distinct] return qselect (left, k-rlen) + rightfor I in range (1, 10 ): print qselect ([11, 8, 4, 1, 5, 2, 7, 9], I)



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.