求解

P1923 【深基9.例4】求第 k 小的数

linjiaze_01 @ 2022-06-06 15:21:15


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class P1923 {
    static int[] box;
    static int k;
    public static void main(String[] args) throws IOException {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        st.nextToken();
        int n = (int) st.nval;
        st.nextToken();
         k = (int) st.nval;

        box = new int[n];
        for (int i = 0; i < n; i++) {
            st.nextToken();
            box[i]= (int) st.nval;
        }

        help(0,box.length-1);
    }

    public static int help(int left,int right){
        int i=left;
        int j=right;
        int mid=(left+right)/2;
        do{
            while (box[i] < box[mid]){
                i++;
            }
            while (box[j] > box[mid]){
                j--;
            }

            if(i<=j){
                int temp=box[i];
                box[i] = box[j];
                box[j]=temp;
                i++;
                j--;
            }

        }while (i<=j);

        if(k<=j){
            help(left,j);
        }else if(k>=i){
            help(i,right);
        }else {
            System.out.print(box[j+1]);
            return 0;
        }
        return 0;

    }
}

请问哪里错了?


|