0

I've struggling to implement a lexicographical merge sort method for the past couple of days. i've come up to a point were i get: ArrayIndexOutOfBounds error and i dont know why. i f anyone could take a look and tell me what might be wrong.i tried debugging but all the values seem correct.

Reply to aix: it happens at this point temp[index1] = array[min + index1] ; These are the values at that exact time:

array :String[5] (5 entries originating from a text file)
min : 0
max : 1
size : 2
pivot : 0
temp : Comparable<T>[2]  (both entries null)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at Merge.mergeSort(Merge.java:47)
    at Merge.mergeSort(Merge.java:43)
    at Merge.Sort(Merge.java:20)
    at Sort.main(Sort.java:96)

Line 43:

mergeSort(array, pivot + 1, max) ;

Line 47:

temp[index1] = array[min + index1] ;

The code:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Merge 
{
    public static void Sort (LinkedList listIn, int size) throws Exception
    {
        String[] mArray = new String[size] ;
        String textContent = null ;
        File outputFile ;

        //copy the list values in the array
        for (int i = 0 ; i < size ; i++)
        {
             mArray [i] = listIn.get(i).printNode();
        }

        mergeSort(mArray, 0, mArray.length) ;       


    }

    public static <T extends Comparable<? super T>> void mergeSort(T[] array, int min, int max)
    {
        T[] temp ;
        int index1 ;
        int left ;
        int right ;

        // if array is of size 1

        if (min == max)
            return ;

        // find length and midpoint
        int size = max - min + 1 ;
        int pivot = (min + max) / 2 ;
        temp = (T[]) (new Comparable[size]) ;

        mergeSort(array, min, pivot) ;
        mergeSort(array, pivot + 1, max) ;

        for (index1 = 0 ; index1 < size ; index1++)
        {
            temp[index1] = array[min + index1] ;
        }

        left = 0 ;
        right = pivot - min + 1 ;
        for (index1 = 0 ; index1 < size ; index1++)
        {
            if (right <= max - min)
                if (left <= pivot - min)
                    if (temp[left].compareTo(temp[right]) > 0)
                        array[index1 + min] = temp[right++] ;
                    else
                        array[index1 + min] = temp[left++] ;
                else
                    array[index1 + min] = temp[right++] ;
            else
                array[index1 + min] = temp[left++] ;
        }
    }


}
12
  • It would help others to help you if you included the stack trace at the point of the exception. Commented Apr 21, 2012 at 10:36
  • take a look at the edited question. i will have the trace in a bit Commented Apr 21, 2012 at 10:42
  • please include stack trace or part of it which points out the exception.. Commented Apr 21, 2012 at 10:42
  • SO should not be a crowd sourced debugger. Commented Apr 21, 2012 at 10:43
  • Just to mention - this is quick sort, not merge sort Commented Apr 21, 2012 at 10:46

1 Answer 1

1

Your Sort method is sending the wrong values when you first invoke merge. Change mergeSort(mArray, 0, mArray.length) to mergeSort(mArray, 0, mArray.length-1);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.