logo CodeStepByStep logo

EvenBeforeOdd

Language/Type: C# arrays traversals

Write a method named EvenBeforeOdd that accepts an array of integers as a parameter and rearranges its elements so that all even values appear before all odds. For example, if the following array is passed to your method:

int[] numbers = {5, 2, 4, 9, 3, 6, 2, 1, 11, 1, 10, 4, 7, 3};

Then after the method has been called, one acceptable ordering of the elements would be:

                {4, 2, 4, 10, 2, 6, 3, 1, 11, 1, 9, 5, 7, 3}

The exact order of the elements does not matter, so long as all even values appear before all odd values. For example, the following would also be an acceptable ordering:

                {2, 2, 4, 4, 6, 10, 1, 1, 3, 3, 5, 7, 9, 11}

Do not make any assumptions about the length of the array or the range of values it might contain. For example, the array might contain no even elements or no odd elements.

You should not use any temporary arrays to help you solve this problem. (But you may declare as many simple variables as you like, such as ints.) You may not use any other data structures such as strings orLists. You should not use the Sort method in your solution.

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.