会飞的土羊 2019-12-10 14:06 采纳率: 0%
浏览 211

快速排序-错误查找,写了个快速排序,但是一直报角标越界...

快速排序,但是一直报如下错误;
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
代码如下:

/**
 * @Description 快速排序
 **/
public class QuickSort {

    public static void main(String[] args) {
        int[]a = {2,1,5,7,9,0,6,4,3,8};
        doSort(a,0,a.length-1);
        System.out.println(a);
    }

    private static void doSort(int[] a, int lo, int hi) {
        int j = partition(a,lo,hi);
        doSort(a,lo,j-1);
        doSort(a,j+1,hi);
    }


    private static int partition(int[] a, int lo, int hi) {
        int i = lo,j = hi+1;
        int v = a[lo];
        while (true){
            while (a[++i]<v) if(i==hi)break;
            while (a[--j] >v) if(j==lo) break;
            if(i>=j) break;
            swap(a,i,j);
        }
        swap(a,lo,j);
        return j;
    }

    private static void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

}

  • 写回答

1条回答 默认 最新

  • 灵动领域 2019-12-10 15:10
    关注

    package com.ctsing.emsm.test;

    public class Test {

    public static void main(String[] args) {
        int[]a = {2,1,5,7,9,0,6,4,3,8};//下标0-9
        System.out.print("原来的数组a:");
        for(int s:a) {
            System.out.print(s+" ");
        }
        System.out.println();
        doSort(a,0,a.length-1);
    }
    
    private static void doSort(int[] a, int lo, int hi) {
        System.out.println("doSort--lo:"+lo+",hi:"+hi);
        int j = partition(a,lo,hi);
        doSort(a,lo,j-1);
        doSort(a,j+1,hi);
    }
    
    
    private static int partition(int[] a, int lo, int hi) {
        System.out.println("partition--lo:"+lo+",hi:"+hi);
        int i = lo,j = hi+1;
        System.out.println("i:"+i+",j+1:"+j);
        int v = a[lo];
        System.out.println(v);
        while (true){
            while (a[++i]<v) if(i==hi)break;
            System.out.println("while--i:"+i);
            while (a[--j] >v) if(j==lo) break;
            System.out.println("while--j:"+j);
            if(i>=j) break;
            swap(a,i,j);
        }
        swap(a,lo,j);
        System.out.println("获取j:"+j);
        return j;
    }
    
    private static void swap(int[] a, int i, int j) {
        System.out.println("swap:"+i+","+j);
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
        System.out.print("现在的数组a:");
        for(int s:a) {
            System.out.print(s+" ");
        }
        System.out.println();
    }
    

    }

    原来的数组a:2 1 5 7 9 0 6 4 3 8
    doSort--lo:0,hi:9
    partition--lo:0,hi:9
    i:0,j+1:10
    2
    while--i:2
    while--j:5
    swap:2,5
    现在的数组a:2 1 0 7 9 5 6 4 3 8
    while--i:3
    while--j:2
    swap:0,2
    现在的数组a:0 1 2 7 9 5 6 4 3 8
    获取j:2
    doSort--lo:0,hi:1
    partition--lo:0,hi:1
    i:0,j+1:2
    0
    while--i:1
    while--j:0
    swap:0,0
    现在的数组a:0 1 2 7 9 5 6 4 3 8
    获取j:0
    doSort--lo:0,hi:-1
    partition--lo:0,hi:-1
    i:0,j+1:0
    0
    while--i:1
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at com.ctsing.emsm.test.Test.partition(Test.java:32)
    at com.ctsing.emsm.test.Test.doSort(Test.java:17)
    at com.ctsing.emsm.test.Test.doSort(Test.java:18)
    at com.ctsing.emsm.test.Test.doSort(Test.java:18)
    at com.ctsing.emsm.test.Test.main(Test.java:12)

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算