【问题标题】:ArrayIndexOutofBound in QUICK-SORT implementationQUICK-SORT 实现中的 ArrayIndexOutofBound
【发布时间】:2016-06-28 08:23:27
【问题描述】:

我想我的代码应该可以工作,但它在排序、分区和主要方法中抛出 ArrayIndexOutofBounds 异常。

我哪里错了?

代码如下:

import java.util.Scanner;

public class QuickSort 
{   
public void sort(int a[], int low, int high)
{

    if(low < high)
    {
        int q = partition(a, low, high);
        sort(a, low, q-1);
        sort(a, q+1, high);
    }
}
public int partition(int a[], int low, int high)
{
    int pivot=a[high];
    int i= (low-1);
    for(int j=low; j<=high-1 ;j++)
    {
        if (a[j] <= pivot)
        {
            i++;
            exchange(a[i], a[j]);
        }
    }   
    exchange(a[i+1], a[high]);
    return i+1;
}
public void  exchange(int v1,int v2)
{
    int var1=v1;
    int var2=v2;
    var1 = var1 + var2;  
    var2 = var1 - var2;  
    var1 = var1 - var2; 
    //System.out.println(var1);
    //System.out.println(var2);
}
public void printArr(int a[])
{
    int n=a.length;
    System.out.println("SORTED ARRAY");
    System.out.println("-------------------------------------------");
    for(int i=0;i<n;i++)
    {
        System.out.print(a[i]);
        System.out.print("\t");
    }
}

public static void main(String[] args) {
    Scanner sc= new Scanner(System.in); 
    QuickSort obj = new QuickSort();
    // TODO Auto-generated method stub

    System.out.println("Enter the no of elements in array");
    int n= sc.nextInt();
    int arr[] = new int[n];
    System.out.println("Enter the elements of array");
    for(int i=1;i<=n;i++)
        {
            arr[i]=sc.nextInt();
        }


    obj.sort(arr, 1 , arr.length);
    obj.printArr(arr); 

    sc.close();
 }
}

这些是 Eclipse 中的错误

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:2
在 QuickSort.partition(QuickSort.java:17)
在 QuickSort.sort(QuickSort.java:10)
在 QuickSort.main(QuickSort.java:67)

【问题讨论】:

  • 提示:j &lt;= high-1j &lt; high 相同
  • @VinceEmigh 的提示在这里非常合适。首先,您将 arr.length 传递给 arg high。请记住,数组是 0 索引的,因此尝试访问 arr.length 将导致 NPE。你想访问arr.length - 1

标签: java algorithm quicksort divide-and-conquer


【解决方案1】:

您从main 调用sort,并使用arr.length 作为high 参数,并将其传递给partition

partition 你可以

int pivot = a[high];

其实是

int pivot = a[a.length];

Array 中的索引从0array size - 1,这就是导致ArrayIndexOutOfBoundsException 的原因。

更改main

obj.sort(arr, 1 , arr.length);

obj.sort(arr, 1 , arr.length - 1);

【讨论】:

  • 相同的 ArrayIndexOutofBounds 异常。 :(
【解决方案2】:

您的代码有几个错误:

  1. 您使用的数组就像从 1 索引到长度一样。在Java中,第一个元素在[0]-cell中,最后一个在[length-1]-cell中

  2. 您的交换方法不起作用:您正在交换局部变量。在 Java 中,如果您在两个整数上调用方法,该方法会复制作为输入给定的这些变量的值,并且您在方法内进行的任何修改都不会影响原始变量。解决方案:将数组作为方法的输入,以及要交换的两个索引。

  3. 如果两个变量实际上相同,那么这种交换(在 1 ... 中添加两个变量)不起作用。您需要过滤这种情况。 (您使用第三个变量的交换不需要此过滤器)

作为一种设计选择,当在数组上编写方法时,参数(int[] 数组,int from,int to)通常在 'from' 的情况下是包含的,在 ' 的情况下是排除的到'。我还修改了你的代码以适应这个规范。

修复所有这些,您将得到这段代码,它可以在我自己的计算机上正确运行:(我用“//There”cmets 标记了修改)

import java.util.Scanner;

public class QuickSort {
public void sort(int a[], int low, int high) {

    if (low < high - 1) {
        int q = partition(a, low, high);
        sort(a, low, q);// There
        sort(a, q + 1, high);// There
    }
}

public int partition(int a[], int low, int high) {
    int pivot = a[high - 1];
    int i = (low - 1);
    for (int j = low; j < high - 1; j++) {
        if (a[j] <= pivot) {
            i++;
            exchange(a, i, j);
        }
    }
    exchange(a, i + 1, high - 1); // There
    return i + 1;
}

public void exchange(int[] tab, int i1, int i2) // There
{
    if (i1 != i2) {
        tab[i1] = tab[i1] + tab[i2];
        tab[i2] = tab[i1] - tab[i2];
        tab[i1] = tab[i1] - tab[i2];
    }
    // System.out.println(tab[i1]);
    // System.out.println(tab[i2]);
}

public void printArr(int a[]) {
    int n = a.length;
    System.out.println("SORTED ARRAY");
    System.out.println("-------------------------------------------");
    for (int i = 0; i < n; i++) {
        System.out.print(a[i]);
        System.out.print("\t");
    }
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    QuickSort obj = new QuickSort();
    // TODO Auto-generated method stub

    System.out.println("Enter the no of elements in array");
    int n = sc.nextInt();
    int arr[] = new int[n];
    System.out.println("Enter the elements of array");
    for (int i = 0; i < n; i++) // There
    {
        arr[i] = sc.nextInt(); // There
    }

    obj.sort(arr, 0, arr.length); // There
    obj.printArr(arr);

    sc.close();
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多