0

I am working on a project for school and things are going well until i tried to perform a merge sort on my ArrayList.
It will run but then it errors out. The first error of many is Exception in thread "main" java.lang.StackOverflowError. I have looked over the code and cant find out why the error is occurring. It does give me a location ( line 74:first_half = mergeSort(first_half); ) but i don't see the issue.

public static void main(String[] args) throws IOException {

    // URL url = new
    // URL("https://www.cs.uoregon.edu/Classes/15F/cis212/assignments/phonebook.txt");
    FileReader fileReader = new FileReader("TestSort.txt");
    BufferedReader bufferReader = new BufferedReader(fileReader);
    String entry = bufferReader.readLine();

    // Scanner s = new Scanner(url.openStream());

//      int count = 0;

    while (entry != null) {

        // String person = s.nextLine();
        String phoneNum = entry.substring(0, 7);
        String name = entry.substring(9);
        PhonebookEntry newentry = new PhonebookEntry(name, phoneNum);
        phoneBook.add(newentry);
        entry = bufferReader.readLine();
    }
    // ********************Selection
    // Sort*************************************
    ArrayList<PhonebookEntry> sortList = new ArrayList<PhonebookEntry>(phoneBook);

    for (int min = 0; min < sortList.size(); min++) {

        for (int i = min; i < sortList.size(); i++) {
            int res = sortList.get(min).getName().compareTo(sortList.get(i).getName());

            if (res > 0) {
                PhonebookEntry temp = sortList.get(i);
                sortList.set(i, sortList.get(min));
                sortList.set(min, temp);

            }

        }

    }
    for (PhonebookEntry sortentry : sortList) {
        System.out.println(sortentry);
    }


    System.out.println(mergeSort(mergeSortList));

}


// *****************************merge sort******************************************
static int mergecounter = 0;
static ArrayList<PhonebookEntry> mergeSortList = new ArrayList<PhonebookEntry>(appMain.phoneBook);

public static ArrayList<PhonebookEntry> mergeSort(ArrayList<PhonebookEntry> mergeSortLists) {
    if (mergeSortLists.size() == 1) {
        return mergeSortLists;
    }
    int firstHalf = mergeSortLists.size() % 2 == 0 ? mergeSortLists.size() / 2 : mergeSortLists.size() / 2 + 1;

    ArrayList<PhonebookEntry> first_half = new ArrayList<PhonebookEntry>(mergeSortLists.subList(0, firstHalf));
    ArrayList<PhonebookEntry> mergeSortHalf2 = new ArrayList<PhonebookEntry>(
    mergeSortLists.subList(first_half.size(), mergeSortLists.size()));

    System.out.println(++mergecounter);

    first_half = mergeSort(first_half);
    mergeSortHalf2 = mergeSort(mergeSortHalf2);
    return merge(first_half, mergeSortHalf2);
}

public static ArrayList<PhonebookEntry> merge(ArrayList<PhonebookEntry> first_half,
        ArrayList<PhonebookEntry> mergeSortHalf2) {
    ArrayList<PhonebookEntry> returnMerge = new ArrayList<PhonebookEntry>();

    while (first_half.size() > 0 && mergeSortHalf2.size() > 0) {
        if (first_half.get(0).getName().compareTo(mergeSortHalf2.get(0).getName()) > 0) {
            returnMerge.add(mergeSortHalf2.get(0));
            mergeSortHalf2.remove(0);
        }

        else {
            returnMerge.add(first_half.get(0));
            first_half.remove(first_half.get(0));
        }
    }

    while (first_half.size() > 0) {
        returnMerge.add(first_half.get(0));
        first_half.remove(first_half.get(0));

    }
    while (mergeSortHalf2.size() > 0) {
        returnMerge.add(mergeSortHalf2.get(0));
        mergeSortHalf2.remove(mergeSortHalf2.get(0));
    }
    return returnMerge;
}

}
Saif
  • 6,804
  • 8
  • 40
  • 61
Soybean
  • 35
  • 6

1 Answers1

0

My opinion there is no error in code.
How so sure?

I ran you code in my environment and its executed without any error.

With the text file i found at https://www.cs.uoregon.edu/Classes/15F/cis212/assignments/phonebook.txt As input and done a simple implementation for PhonebookEntry

Then why is this error?

First off all try to understand the error, I mean why StackOverflowError occur. As there are lots of I am not going to explain this But please read the top answer of this two thread and i am sure you will know why this happen.

Thread 1: What is a StackOverflowError?
Thread 2: What actually causes a Stack Overflow error?

If you read those I hope you understand the summury is You Ran Out Of Memory.

Then why I didnt got that error: Possible reason is

In my environment I configured the jvm to run with a higher memory 1024m to 1556m (as eclipse parameter)

Now lets analyze your case with solution:

  1. Input: you have big input here ( 50,000 )

    To check you code try to shorten the input and test.

  2. You have executed two algorithm in a sigle method over this big Input: When a method execute all its varibles stay in the memory untill it complete its execution. so when you are calling merge sort all previouly user vairables and others stay in the memory which can contribute to this situation

    Now if you use separated method and call them from the main method like write an method for selection sort, all its used varible will go out of scope and possibly be free (if GC collect them) after the selection sort is over.

    So write two separated method for reading input file and selection sort. And Please Please close() those FileReader and BufferedReader.

  3. Get out of those static mehtod . Make them non static create and object of the class and call them from main method

So its all about code optimization

And also you can just increase the memory for jvm and test by doing like this java -Xmx1556m -Xms1024m when ruining the app in command line

BTW, Thanks for asking this this question its gives me something to think about

Community
  • 1
  • 1
Saif
  • 6,804
  • 8
  • 40
  • 61
  • Thank you. this helped alot. The issue was with my calls. I was checking with recursion if something ==0 but it was always 0 as it was never passed the Original arraylist. Fixed the code and it now works. You helped me find the issue. – Soybean Nov 11 '15 at 19:37
  • Happy to know that it solved now :) . Although I could provide any specific help. – Saif Nov 12 '15 at 04:07