3.0k views
1 vote
Write a recursive Java method that rearranges an array of

integer values so that all the even values appear before all the
odd values. The method should change the array permanently.

User Ramsinb
by
7.5k points

1 Answer

4 votes

Final answer:

To rearrange an array of integer values so that all the even values appear before all the odd values, you can use a recursive Java method.

Step-by-step explanation:

To rearrange an array of integer values so that all the even values appear before all the odd values, you can use a recursive Java method. Here is a step-by-step explanation of how to do it:

  1. Create a recursive method that takes in the array, a starting index, and an ending index as parameters.
  2. In the method, check if the starting index is greater than or equal to the ending index. If so, return.
  3. If the value at the starting index is even, increment the starting index and recursively call the method.
  4. If the value at the ending index is odd, decrement the ending index and recursively call the method.
  5. If the value at the starting index is odd and the value at the ending index is even, swap the values.
  6. After swapping or skipping elements, recursively call the method with updated starting and ending indices.

By following these steps, the array will be rearranged so that all the even values appear before all the odd values.

User Rubens Farias
by
7.5k points