Question: I had an interview question which i could not solve. 

Write method (not a program) in Java Programming Language that will move all even numbers on the first half and odd numbers to the second half in an integer array. 

E.g. Input = {3,8,12,5,9,21,6,10}; Output = {12,8,6,10,3,5,9,21}.

The method should take integer array as parameter and move items in the same array (do not create another array). The numbers may be in different order than original array. This is algorithm test, so try to give as efficient algorithm as you can (possibly linear O(n) algorithm). Avoid using built in functions/API. \*

Also some basic intro to what is data structure efficiency

Answer: 
// to do this in one for loop 

```
public static void evenodd(int[] integer) {

    int i = 0, temp = 0;
    int j = integer.length - 1;

    while (j >= i) {
        // swap if found odd even combo at i and j
        if (integer[i] % 2 != 0 && integer[j] % 2 == 0) {
            temp = integer[i];
            integer[i] = integer[j];
            integer[j] = temp;
            i++;
            j--;

        } else {
            if (integer[i] % 2 == 0) {
                i++;
            }
            if (integer[j] % 2 == 1) {
                j--;
            }

        }

    }
} 

```
Use two counters i=0 and j=a.length-1 and keep swapping even and odd elements that are in the wrong place.

```
  public int[] evenOddSort(int[] a) {
        int i = 0;
        int j = a.length - 1;
        int temp;
        while (i < j) {
            if (a[i] % 2 == 0) {
                i++;
            } else if (a[j] % 2 != 0) {
                j--;
            } else {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
                i++;
                j--;
            }
        }
        return a;
    }

```