Elegant Python-A quick select solution for big Data TOPK problems

Source: Internet
Author: User

The TOPK problem, which is finding the largest number of K, is very common, such as finding the hottest 10 keywords from 10 million search records.

Method One:

First, then the number of the first k is truncated.

Time complexity: O (N*logn) +o (k) =o (N*LOGN).


Method Two:

Minimum heap.

Maintain the smallest heap with a capacity of K. According to the minimum heap nature, the heap top must be the smallest, if smaller than the heap top, then the direct pass, if greater than the heap top, then replace the heap top, and heapify the heap, wherein heapify time complexity is logk.

Time complexity: O (k + (n-k) *logk) =o (N*LOGK)


Method Three:

The protagonist of this paper. The quick Select algorithm. In fact, it is similar to the quick row. The difference is that the quick select only needs to go in one direction per trip.

Time complexity: O (n).

def qselect (a,k):    If Len (a) <k:return A    pivot = a[-1] Right    = [Pivot] + [x for x in a[:-1] if x>=pivot]
   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<pivot]        return Qselect (left, K-rlen) + rightfor I in range (1):    print Qselec T ([11,8,4,1,5,2,7,9], i)



Elegant Python-A quick select solution for big Data TOPK problems

Related Article

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.