Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why does this MergeSort implementation not receive IndexOutOfBondsExpception

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does this MergeSort implementation not receive IndexOutOfBondsExpception

    Hi there I have this implamentation of the MergeSort algorithm from a text book and it looks like this:



    public static void mergeSort(int[] array) {
    if(array.length > 1) {
    int[] left = leftHalf(array);
    int[] right = rightHalf(array);
     
    mergeSort(left);
    mergeSort(right);
     
    merge(array, left, right);
    }
    }

    The leftHalf and rightHalf respectively return the left and right halves of the original array, no tricks there!

    And then I have the merge method:

    public static void merge(int[] result, int[] left, int[] right) {
     
    int i1 = 0;
    int i2 = 0;
     
    for(int i = 0; i < result.length; i++) {
    if(i2 >= right.length || (i1 < left.length && left[i1] <= right[i2])) {
     
    result[i] = left[i1];
    i1++;
    } else {
     
    result[i] = right[i2];
    i2++;
    }
    }
    }
    }

    So as far as I can understand if you call this mergeSort with an array of:

    int[] list = {42, 17, 33, 55};

    Then we get mergeSort doing the following calls:

    mergeSort(list);
    left = 42, 17
    right = 33, 55
    mergeSort(left);
    left = 42;
    right = 17;
    mergeSort(left); // Nothing happens bottoms out
    mergeSort(right); // Nothing happens bottoms out
    merge(array, left (42), right (17));

    So as far as I can see we are calling merge with an array of length four (array), and two arrays of length 1.
    So why do we not get a call that is out of bounds in the two arrays of length of 1, in our merge method?
    As far as I can see, after two traversals of the for loop, we would end up with both i1 = i2 = 1.

    And then we would get a call right[1] which is not legal.

    So what is wrong?


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does this MergeSort implementation not receive IndexOutOfBondsExpception

    Might there be a helpful soul out there?

    Bump!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Why does this MergeSort implementation not receive IndexOutOfBondsExpception

    Can you post a complete program that compiles and executes and shows the problem? Make sure the code is properly formatted. Logically nested statements should be indented 3-4 spaces.

    Try debugging the code by adding some calls to the println() method that prints out the values of the variables as the code executes.
    For arrays: System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Why does this MergeSort implementation not receive IndexOutOfBondsExpception

    If I understand correctly you are asking why there is no out of bounds when it breaks it down to a single element array. The answer to your question is that this is a recursive method. It calls itself from within andruns the code inside each time. As you can see at the beginning of the method it checks to make sure the array size contains more than 1 element otherwise it does not run the code.

    What the code is doing
    1. Passing an array to mergeSort method
    2. Checks the size of the array if the array contains more than 1 element it runs code
    3. Breaks the array into 2 different array a left and a right side
    4. Calls mergeSort with left side of array (mergeSort(leftSide))
    5. Repeats steps 2 - 4 until array's are broken down to 1 element then returns to previous stack to continue running code.
    6. Does same thing as before but now with the right side of the array (mergeSort(rightSide))

    Each recursive call adds to the stack and once the code is completely executed in a stack it returns to the previous stack to execute the rest of the code. As you can see you never get the index out of bounds from right[1] because that case does not happen in the code. As soon as the array contains only 1 element it skips the code that breaks the array in half.

    With this type of problem it makes it makes things a lot easier when you draw them out on paper and understand what the code is doing.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,606
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: Why does this MergeSort implementation not receive IndexOutOfBondsExpception

    Here is a general trace of what would happen in your case. Each * should be considered one extra level of recursion:

    int[] list = {42, 17, 33, 55};
     
    mergeSort(array0 = list)
    left0 = {42,17}
    right0 = {33,55}
    *mergeSort(array1 = left0)
    *left1 = {42}
    *right1 = {17}
    **mergeSort(array2 = left1) -- Exit immediately
    **mergeSort(array3 = right1) -- Exit immediately
    **merge(result = array1, left = left1, right = right1) -- result.length = 2
    *mergeSort(array4 = right0)
    *left2 = {33}
    *right2 = {55}
    **mergeSort(array5 = left2) -- Exit immediately
    **mergeSort(array6 = right2) -- Exit immediately
    **merge(result = array4, left = left2, right = right2) -- result.length = 2
    merge(result = array0, left = left0, right = right0) -- result.length = 4

    I think you are confused with what array is being sent as the result parameter to the merge method when recursively checking each half of the array. The result parameter is the array before it was split into left and right, not after, so the length of the result array parameter can never be less than 2.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Help with using mergesort to sort a list of names alphabetically?
    By BearBearBear in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 27th, 2012, 09:10 AM
  2. MergeSort won't work
    By joshft91 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 08:44 PM
  3. MergeSort not working Helppp please :(
    By colombianita2089 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 17th, 2010, 08:56 PM
  4. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 04:48 PM
  5. MergeSort
    By mgutierrez19 in forum Algorithms & Recursion
    Replies: 3
    Last Post: March 3rd, 2010, 07:46 PM