Java求助,改装快排但是最后两个还是超时
查看原帖
Java求助,改装快排但是最后两个还是超时
1315126
Sagiri1210楼主2024/7/12 11:14

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

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;
    }
}

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

2024/7/12 11:14
加载中...