Search code examples
javaalgorithmmergesort

Java Mergesort Implementation having a stackoverflow error


I'm not quite sure why my merge sort implementation is getting a stackoverflow error. I've reviewed the code multiple times but I'm not getting why this is happening. I tried debugging but have had trouble understanding why the values 0 and 4 (the first and last index of array) never change when I walk through the code. And then finally it throws the stack overflow error.


Solution

  • The first recursive call to mergesort (line 17?) should use "start, mid", not "start, end". That puts you in an endless loop, so you eventually run out of stack.

    Trust your debugger.