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)