Java 快排 RE 求助
查看原帖
Java 快排 RE 求助
444627
Kit_ZKY楼主2024/9/15 18:03
import java.io.*;
import java.util.*;

public class QuickSort{
    public static void main(String[] args) {
        QReader in = new QReader();
        QWriter out = new QWriter();

        int N = in.nextInt();
        int[] p = new int[N];
        for (int i = 0; i < N; i++) {
            p[i] = in.nextInt();
        }
        quick_Sort(p, 0, N-1);

        for (int j = 0; j < N; j++) {
            System.out.print(p[j]+" ");
        }
    }
    static void quick_Sort(int[] q,int l,int r){
        if(l>=r){return;}

        int x = q[l]; 
        int i = l-1, j= r+1;

        while(i<j){
            do { 
                i++;
            } while (q[i]<x);
            do { 
                j--;
            } while (q[j]>x);
            if(i<j){
                int temp = q[i];
                q[i] = q[j];
                q[j] = temp;
            }
        }

        quick_Sort(q, l, j);
        quick_Sort(q, j+1, r);

    }
}

class QReader {
    private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private StringTokenizer tokenizer = new StringTokenizer("");
 
    private String innerNextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            return null;
        }
    }
 
    public boolean hasNext() {
        while (!tokenizer.hasMoreTokens()) {
            String nextLine = innerNextLine();
            if (nextLine == null) {
                return false;
            }
            tokenizer = new StringTokenizer(nextLine);
        }
        return true;
    }
 
    public String nextLine() {
        tokenizer = new StringTokenizer("");
        return innerNextLine();
    }
 
    public String next() {
        hasNext();
        return tokenizer.nextToken();
    }
 
    public int nextInt() {
        return Integer.parseInt(next());
    }
 
    public long nextLong() {
        return Long.parseLong(next());
    }
}
 
class QWriter implements Closeable {
    private BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
 
    public void print(Object object) {
        try {
            writer.write(object.toString());
        } catch (IOException e) {
            return;
        }
    }
 
    public void println(Object object) {
        try {
            writer.write(object.toString());
            writer.write("\n");
        } catch (IOException e) {
            return;
        }
    }
 
    @Override
    public void close() {
        try {
            writer.close();
        } catch (IOException e) {
            return;
        }
    }
}

不知道哪里出了问题呀

2024/9/15 18:03
加载中...