这种排序算法有什么问题? (越界异常)

时间:2016-02-08 22:36:09

标签: java quicksort

错误如下:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at QuickSort.partition(QuickSort.java:39)
    at QuickSort.quickSort(QuickSort.java:19)
    at QuickSort.sort(QuickSort.java:13)
    at QuickSortTest.main(QuickSortTest.java:15)

我无法弄清楚它为什么会超出界限。

 if (orig[q] >= orig[q - 1]) {

我正在测试这个以获得乐趣。我知道使用这种枢轴可能不会做任何事情。我试图在此区间内找到数组中的最大值 - >原始长度的原始长度/ sqrt * log 2(原始长度),然后是最接近该最大值的33%的值,并将其设置为我的枢轴。然后通常快速排序。

import java.lang.*;

public class QuickSort {
    int[] ary;

    public QuickSort(int[] num) {
        ary = num;
    }

    public int[] sort() {
        int low = 0;
        int high = ary.length - 1;
        quickSort(ary, low, high);
        return ary;
    }

    private void quickSort(int[] orig, int low, int high) {
        int save = partition(orig, low, high);
        if (low < (save - 1)) {
            quickSort(orig, low, (save - 1));
        }
        if (save < high) {
            quickSort(orig, save, high);
        }
    }

    private int partition(int[] orig, int low, int high) {
        int i = low;
        int j = high;
        int temp;
        int large = 0;
        int swapspot = 0;
        int need = orig.length / ((int) (Math.sqrt(ary.length)) * ((int) (Math.log(ary.length) / (Math.log(2)))));
        for (int q = need; q > need / 2 - 1; q--) {
            if (orig[q] >= orig[q - 1]) {
                orig[q] = large;
            } else {
                orig[q - 1] = large;
            }
            if (q == need / 2) {
                int saver = (int) (large * .33);
                for (int h = 0; h < orig.length; h++) {
                    if (orig[h] >= saver) {
                        swapspot = orig[h];
                    }
                }
            }
        }
        while (i <= j) {
            while (orig[i] < swapspot) {
                i++;
            }
            while (orig[j] > swapspot) {
                j--;
            }
            if (i <= j) {
                temp = orig[i];
                orig[i] = orig[j];
                orig[j] = temp;
                i++;
                j--;
            }
        }
        return i;
    }
}

这是主要方法

class QuickSortTest{

   public static void main(String[] args){

      int[] num = new int[100];      

      for(int i = 0; i < num.length; i++){
         num[i] = (int)(Math.random()*10000);

      }

      long begin = System.currentTimeMillis();   

      QuickSort quick = new QuickSort(num);
         quick.sort();    

      long finish = System.currentTimeMillis();


      for(int i = 0; i < num.length; i++){
         System.out.println(num[i]);  
      }
      System.out.println((finish-begin) + " ms");
   } 
}

1 个答案:

答案 0 :(得分:2)

只需查看问题的这两行:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
if (orig[q] >= orig[q - 1]) {

我可以告诉你,q为0,显然没有原点[-1]

更新,由@kaanyılmaz添加:

有更多解释(实际上它不需要,因为答案已经足够好,但你要问的是编程的基础知识)

 q=0 

在循环开始时然后你想分配;

 q=-1

不太可能发生,因为你的CPU无法达到oriq [-1],因为你不能算苹果像-1,0,1,2 ......