Java求助,改装快排但是最后两个还是超时

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

Sagiri1210 @ 2024-07-12 11:14:48

下面是我的代码,改造了一个快速排序

import java.util.Scanner;

public class Main {
    public static int k;
    public static long[] a;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        k = sc.nextInt();
        a = new long[n];
        sc.nextLine();
        String s = sc.nextLine();
        sc.close();
        String[] split = s.split(" ");
        for (int i = 0; i < n; i++){
            a[i] = Long.parseLong(split[i]);
        }
        quickSort(0,n-1);
        System.out.println(a[k]);
    }
    public static void quickSort( int l, int r){
        int i = l, j = r;
        long x = a[(l+r)/2];
        while (i<=j){
            while (a[i]<x){
                i++;
            }
            while (a[j]>x){
                j--;
            }
            if (i<=j){
                long temp = a[i];
                a[i] = a[j];
                a[j] = temp;
                i++;
                j--;
            }
        }
        if (k<=j){
            quickSort(l,j);
        }else if (k>=i){
            quickSort(i,r);
        }else if (k>=j &&k<=i)
            return;
    }
}

为什么最后两个测试点还是超时???求救求救


by stiens @ 2024-07-15 09:58:43

我之前用Java,这种题数据量超过一百万Java用快读都超时,更别说不用快读了,现在转战c++了


|