【问题标题】:java.lang.IllegalArgumentException in QuickSort method [closed]QuickSort 方法中的 java.lang.IllegalArgumentException [关闭]
【发布时间】:2018-11-22 19:53:58
【问题描述】:

我正在遵循以下伪代码:

function quicksort(array)
    if length(array) > 1
        pivot := select any element of array
        left := first index of array
        right := last index of array
        while left ≤ right
            while array[left] < pivot
                left := left + 1
            while array[right] > pivot
                right := right - 1
            if left ≤ right
                swap array[left] with array[right]
                left := left + 1
                right := right - 1
        quicksort(array from first index to right)
        quicksort(array from left to last index)

但是当我尝试在 java 中实现它时,我插入了代码:

import java.util.Arrays;

public class ALGQuickSort {

public static void main(String[] args) {
    int[] array = {6, 3, 4, 8, 9, 10, 1};
    quickSort(array);
    System.out.println(Arrays.toString(array));
}

public static void quickSort(int[] array) {
    int pivot = array[array.length - 1];
    if (array.length > 1) {
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            while (array[left] < pivot) {
                left++;
            }
            while (array[right] > pivot) {
                right--;
            }
            if (left <= right) {
                swap(array[left], array[right]);
                right--;
                left++;
            }
            int[] array1 = Arrays.copyOfRange(array, 0, right);
            int[] array2 = Arrays.copyOfRange(array, left, array.length - 1);
            quickSort(array1);
            quickSort(array2);

        }
    }

}

public static void swap(int a, int b) {
    int aux = a;
    a = b;
    b = aux;
}

}

系统在屏幕上显示以下错误:

线程“主”java.lang.IllegalArgumentException 中的异常:5 > 4 在 java.util.Arrays.copyOfRange(Arrays.java:3591) 在 alg.quicksort.ALGQuickSort.quickSort(ALGQuickSort.java:43) 在 alg.quicksort.ALGQuickSort.quickSort(ALGQuickSort.java:44) 在 alg.quicksort.ALGQuickSort.main(ALGQuickSort.java:21) C:\Users\Alex\AppData\Local\NetBeans\Cache\8.2\executor-sn-ps\run.xml:53: Java 返回:1

错误在以下行:

int[] array2 = Arrays.copyOfRange(array, left, array.length - 1);

有人可以帮帮我吗?

【问题讨论】:

  • 请阅读 Eric Lippert 的 "How to debug small programs"
  • 你能发布完整的错误吗?
  • @pavlos163 我更新了!!
  • 您正在围绕数组进行复制,而不是在内存中的单个集合上进行旋转。这使您的索引无法正常工作。该错误告诉您无法从索引 5 复制到索引 4,因为 5 大于 4。

标签: java algorithm sorting quicksort


【解决方案1】:

对您的快速排序算法的改进/更正:

  1. 更正您的交换方法:您使用的交换方法将不起作用。
  2. 递归调用应该在for循环之外:您的伪代码是正确的,但在您的实现中并非如此。
  3. 将 Pivot 放在正确的位置,然后递归调用现在(肯定)不应包含枢轴元素的子数组。 在 while 循环之后,您确定 right+1 == left (想一想,你就会明白为什么这是真的)。现在将数组 [left] 与枢轴元素交换,并对 2 个不同的子数组进行递归调用(枢轴的左子数组是beginIndex..right,枢轴的右子数组是left+1..endIndex,我假设你需要排序array[beginIndex..endIndex])
  4. 避免复制:最好避免复制数组的一部分(您可以传递startIndexendIndex 来表示要排序的子数组)
  5. 使用随机快速排序:如果您不总是选择最后一个元素作为枢轴也会更好(您可以在这里做的是在开始排序之前选择任何随机元素然后交换它使用数组的最后一个元素。使用此策略,您不必对现有代码进行太多更改)这种选择随机元素作为枢轴要好得多。有关详细信息,请参阅this 链接。
  6. 将交换方法设为私有:与算法无关,但最好将交换方法设为私有,因为您可能不打算从此类外部调用它。

如果您打算交换索引为 index1 和 index2 的数组元素,则以下代码将起作用:

public static void swap(int[] array, int index1, int index2) {
    int aux = array[index1];
    array[index1] = array[index2];
    array[index2] = aux;
}

以下是包含上述所有建议更改的最终代码:

public static void main(String[] args) {
    int[] array = {6, 30, 7, 23, 4, 8, 9, 10, 1, 90};
    quickSort(array, 0, array.length - 1);
    System.out.println(Arrays.toString(array));
}

public static void quickSort(int[] array, int beginIndex, int endIndex) {
    // System.out.println("called quick sort on the following : " + beginIndex + " " + endIndex);
    int arrayLength = endIndex - beginIndex + 1;
    int pivot = array[endIndex];
    if (arrayLength > 1) {
        int left = beginIndex;
        int right = endIndex - 1;
        while (left <= right) {
            // System.out.println(left + " " + right);
            while (left <= endIndex && array[left] < pivot) {
                left++;
            }
            while (right >= beginIndex && array[right] > pivot) {
                right--;
            }
            if (left <= right) {
                swap(array, left, right);
                right--;
                left++;
            }

        }
        swap(array, left, endIndex); // this is crucial, and you missed it
        if (beginIndex < right) {
            quickSort(array, beginIndex, right);
        }
        if (left + 1 < endIndex) {
            quickSort(array, left + 1, endIndex);
        }
    }

}

private static void swap(int[] array, int index1, int index2) {
    int aux = array[index1];
    array[index1] = array[index2];
    array[index2] = aux;
}

【讨论】:

  • 非常感谢,一切都解释得很好,我明白了一切!谢谢!!
【解决方案2】:

首先是两行

int[] array1 = Arrays.copyOfRange(array, 0, right);
int[] array2 = Arrays.copyOfRange(array, left, array.length - 1);

错了。 您不得复制数组。这将阻止快速排序算法工作,因为您在递归调用中对临时副本进行排序。排序后的子数组将被丢弃。

您程序中的另一个错误引发了异常:Arrays.copyOfRange 的第三个参数是独占。 IE。它不会将元素from 复制到to,而是将from 复制到to - 1。但是您已经从上限中减去了一个,并且在快速排序中,可能会发生其中一个子数组具有零大小。在这种情况下,要复制的范围变为负数。那是个例外。

还有第三个错误:您交换了伪代码的前两行。这将在空数组上崩溃。


最后,你不能以这种方式实现快速排序算法,如伪代码所示因为Java(不像C)不支持数组切片。

您需要修改算法,将其拆分为两种方法:

  • 一种递归排序数组切片的方法
    请注意,我将数组开头和结尾替换为 fromto

    public static void quickSort(int[] array, int from, int to) {
      if (array.length <= 1)
        return;
      int pivot = array[to];
      int left = from;
      int right = to;
      while (left <= right) {
        while (array[left] < pivot)
          left++;
        while (array[right] > pivot)
          right--;
        if (left <= right) {
          swap(array[left], array[right]);
          right--;
          left++;
        }
        quickSort(array, from, right);
        quickSort(array, left, to);
      }
    }
    
  • 还有一种对整个数组排序的方法,它为整个数组调用上述方法。

    public static void quickSort(int[] array) {
      return quickSort(array, 0, array.length - 1);
    }
    

【讨论】:

    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多