Topk Algorithm Based on quick sorting

Source: Internet
Author: User

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? ") ;}}
   
  
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.