简体   繁体   中英

IndexOutOfBoundsException and quicksort algorithm

I am having trouble figuring out why my code has an IndexOutOfBoundsException . I was wondering if someone could be my extra pair of eyes to help me catch the error. I am trying this out by hand but I think I am missing something as I go through the code.

code:

package Algorithms;

import java.util.ArrayList;

public class quickSort {

    public static ArrayList<Integer> mergeArrayList(ArrayList<Integer> min, int pivot, ArrayList<Integer> max) {
        ArrayList<Integer> mergedList = new ArrayList<Integer>();
        //mergedList.addAll(min);
        for (int i : min) {
            mergedList.add(i);
        }
        mergedList.add(pivot);
        //mergedList.addAll(max);
        for (int j : max) {
            mergedList.add(j);
        }
        return mergedList;
    }


    public static ArrayList<Integer> quicksort(ArrayList<Integer> array, int min, int max) {
        if (array.size() <= 1) {
            return array;
        }
        int pivot = (min + max)/2;
        ArrayList<Integer> less = new ArrayList<Integer>();
        ArrayList<Integer> greater = new ArrayList<Integer>();
        for (int i : array) {
            if (i <= array.get(pivot)) {
                less.add(i);
            }
            else {
                greater.add(i);
            }
        }
        return mergeArrayList(quicksort(less,min,pivot - 1), array.get(pivot), quicksort(greater, pivot + 1, max));
    }

    public static void main(String[] args) {
        ArrayList<Integer> arr1 = new ArrayList<Integer>();
        int[] arr = {1,2,3,4,5};
        for (int i : arr) {
            arr1.add(i);
        }
        System.out.println(quicksort(arr1,0,arr1.size() - 1));
    }
}

error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at Algorithms.quickSort.quicksort(quickSort.java:30)
    at Algorithms.quickSort.quicksort(quickSort.java:37)
    at Algorithms.quickSort.main(quickSort.java:46)

Add this line for debugging purposes at the start of your quicksort method.

public static ArrayList<Integer> quicksort(ArrayList<Integer> array, int min, int max) 
{
    System.out.println(Arrays.toString(array.toArray()) + " -> " + min + " " + max);
    ...
}

You will see exactly what is happening and when/where it goes wrong.

quicksort(greater,pivot + 1, max) in the return will result in:

int pivot = (max + pivot + 1) /2; //pseudocode

pivot will be greater than the length of the array,(which is half the original one) resulting in an out of bounds exception when you ask for array.get(pivot)

the problem is the min & max

I understand that you use them as indexes in the array that you send, But - you send the less & greater which are smaller than the original array.

so, for your example , after the first call to quicksort , pivot = 2 this part quicksort(greater, pivot + 1, max) will send the greater with value of 3,4 so this create you exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM