Similar to quick sorting, first select a shard. If the index of the Shard is just K, the shard and the number on the left constitute the top-K small data; if the index is greater than K, the top-K small data is on the left of the index, and the top-K is selected from index-1 and number recursively. if the index is <K, select top-(k-index) small data from the right of the index.
CodeAs follows:
public class topk_quick {public static int partition (int A [], int low, int high) {A [0] = A [low]; int primary key = A [low]; while (low
= primary key) -- high; A [low] = A [High]; while (low
K) // The number of K is on the left of the index {Index = selectk (A, start, index-1, k) ;}} Return Index ;} public static void main (string ARGs []) {int K = 0; int A [] = {, 2 2, 19}; If (k> 0 & K <=. length-1) {selectk (A, 1,. length-1, k); display (A, K);} else {system. out. println ("Are you kidding me? ") ;}}