我不知道为什么对于另一个数组会抛出异常?

huangapple 未分类评论47阅读模式
英文:

I don't know why it throws exception for the other array?

问题

以下是翻译好的部分:

每当我尝试在以下数组上运行此代码时

int arr[]= {64,32,85,11,56,23};

它可以正常运行,但是当我尝试在以下数组上运行相同的代码时:

int arr[] = {43,32,22,78,63,57,91,13};

它会抛出 `ArrayIndexOutOfBoundsException` 异常。为什么会抛出这个异常?

public class QuickSort {
    static void swap(int arr[], int index1, int index2){
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp ;
    }
    static int partition(int arr[], int start, int end){
        int pivot = arr[start], i = start, j = end ;
        while(i <= j){
            while(arr[i] <= pivot) i++ ;
            while(arr[j] > pivot)   j-- ;
            if(i <= j){
                swap(arr,i,j);    i++ ; j-- ;
            }
        } 
        swap(arr,start,j);
        return j ;
    }
    static void quick_sort(int arr[], int start, int end){
        if(start < end){
            int j = partition(arr,start,end);
            quick_sort(arr, start, j);
            quick_sort(arr, j+1, end);
        }
    }
    static void print(int arr[]){ for(int i = 0; i< arr.length ; i++ ) System.out.print(arr[i] + " ");}

    public static void main(String ...s){
        int arr[] = {43,32,22,78,63,57,91,13};
        quick_sort(arr, 0, arr.length-1);
        print(arr);
    }
}

异常堆栈跟踪:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
        at QuickSort.partition(Quick Sort.java:11)
        at QuickSort.quick_sort(Quick Sort.java:22)
        at QuickSort.quick_sort(Quick Sort.java:24)
        at QuickSort.quick_sort(Quick Sort.java:24)
        at QuickSort.main(Quick Sort.java:32)
英文:

Whenever I try to run this code on the array

int arr[]= {64,32,85,11,56,23};

it runs fine but when I try to run the same code on this array

int arr[] = {43,32,22,78,63,57,91,13};

it throws ArrayIndexOutOfBoundsException. Why is this exception thrown?

public class QuickSort {
	static void swap(int arr[], int index1, int index2){
		int temp = arr[index1];
		arr[index1] = arr[index2];
		arr[index2] = temp ;
	}
	static int partition(int arr[], int start, int end){
		int pivot = arr[start], i = start, j = end ;
		while(i &lt;= j){
			while(arr[i] &lt;= pivot) i++ ;
			while(arr[j] &gt; pivot)	j-- ;
			if(i &lt;= j){
				swap(arr,i,j);	i++ ; j-- ;
			}
		} 
		swap(arr,start,j);
		return j ;
	}
	static void quick_sort(int arr[], int start, int end){
		if(start &lt; end){
			int j = partition(arr,start,end);
			quick_sort(arr, start, j);
			quick_sort(arr, j+1, end);
		}
	}
	static void print(int arr[]){ for(int i = 0; i&lt; arr.length ; i++ ) System.out.print(arr[i] + &quot; &quot;);}
	public static void main(String ...s){
		int arr[] = {43,32,22,78,63,57,91,13};
		quick_sort(arr, 0, arr.length-1);
		print(arr);
	}
}

Exception Stack Trace :

Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
        at QuickSort.partition(Quick Sort.java:11)
        at QuickSort.quick_sort(Quick Sort.java:22)
        at QuickSort.quick_sort(Quick Sort.java:24)
        at QuickSort.quick_sort(Quick Sort.java:24)
        at QuickSort.main(Quick Sort.java:32)

huangapple
  • 本文由 发表于 2020年4月9日 19:06:30
  • 转载请务必保留本文链接:https://java.coder-hub.com/61119732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定