2

I'm trying to reverse an integer array using a recursive method. I'm really bad with recursion so far, and I was wondering if someone could help me with this problem that I'm having.

Here is my code so far:

public static int[] reverseArray(int[] array, int startIndex, int endIndex){
    int[] tempArray = array;
    if(tempArray[startIndex] == array[endIndex]){
        return tempArray;
    }
    else{
        tempArray[startIndex] = array[endIndex];
        reverseArray(array, startIndex + 1, endIndex - 1);
        return tempArray;
    }
}
2
  • You have the correct recursive call in there - just think about what you need to do to array[startIndex] and array[endIndex] to reverse the array overall. Also, think about the stopping condition - why does the equality of element values matter? Commented Oct 25, 2015 at 21:15
  • BTW, tempArray is simply an alias for array - you aren't making a copy of the array by doing that. It only serves to make the code more complicated. Commented Oct 25, 2015 at 21:17

1 Answer 1

4

Your recursive logic is fine: to reverse an array, we reverse the first and last element and do that again on the array without those elements. That means we need to swap the first and the last element together and call the method again, incrementing the first index and decrementing the last index. In your code, however, you are only changing tempArray[startIndex] and not tempArray[endIndex].

The base condition is wrong though: there is nothing to do, not when the first element is equal to the last element, but when the first index is greater or equal than the last index (if it is equal, there is only one element to consider and so the reverse is also the same element).

Putting this into code, this turns into:

private static int[] reverseArray(int[] array, int startIndex, int endIndex) {
    if (startIndex >= endIndex) { // base condition, nothing to do when there is one or no element to consider
        return array;
    }
    // swap array[startIndex] and array[endIndex]
    int temp = array[startIndex];
    array[startIndex] = array[endIndex];
    array[endIndex] = temp;
    // recurse with the decreasing bounds
    return reverseArray(array, startIndex + 1, endIndex - 1);
}

Note that I removed the declaration of the tempArray: we can consider directly array. Then, we can add a utility method:

public static int[] reverseArray(int[] array){
    return reverseArray(array.clone(), 0, array.length - 1);
}

Note that I made this method public and the other one private: the one you will want to call will be this one since it hides the recursive implementation. I added a call to clone() in there so as not to modify the given array when calculating the reverse.

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.