title: Rearrange even and odd in an array
body: <p>I have written a program to place all even elements of the array to the left and odd ones to the right half of the array. The order of elements is not of concern. I was wondering if there is more efficient algorithm than this. My worst case complexity is O(n/2). Here is the code.</p>
  
  <pre><code>// Program to shift all even numbers in an array to left and odd to the right. Order of digits is not important.
  
  import java.util.*;
  import java.lang.*;
  import java.io.*;
  
  
  class Rearrange
  {
  public static void main (String[] args) throws java.lang.Exception
  {
      // your code goes here
      int[] array = new int[] {1,2,3,4,5,6,7};
  
      // keep two pointers.
      int odd, even;
      odd = 0; 
      even = array.length-1;
      int i;
  
      // Code to re-arrange the contents of the array
      i=0;
      while(i&lt;array.length){
          if(array[i]%2!=0){
              odd = i;
              break;
          }
          i++;    
      }
  
      i=array.length-1;
      while(i&gt;=0){
          if(array[i]%2==0){
              even = i;
              break;
          }
          i--;    
      }
  
      while(odd&lt;even){
          if((array[odd]%2!=0) &amp;&amp; (array[even]%2==0)){
              // swap contents
              array[odd] = array[odd] + array[even];
              array[even] = array[odd] - array[even];
              array[odd] = array[odd] - array[even];
              odd++;
              even--;
          }
  
          else if(array[odd]%2==0){
              odd++;
          }
  
          else if(array[even]%2!=0){
              even--;
          }
  
          else
              continue;
      }
      for(int val : array)
          System.out.println(val+" ");
  
  }
  }
  </code></pre>