Segregate an array of 0s and 1s I think that my code contains a lot of if and else statements that are probably not required.I suppose that this code could be condensed to a shorter form where the logic applied would look clearer than it does now. 
Suggestions are welcome and any help in refactoring would be highly appreciated.

public class Segregate{
    public static void doSegregation(int[] a){
        int i = 0;
        int j = a.length-1;

      while(i&lt;=j){
        if(a[i]==a[j]){
          if(a[i]==1 &amp;&amp; a[j]==1){
            j--;
          }
        else if(a[i]==0 &amp;&amp; a[j]==0){
            i++;
        }
    }
    else{
        if(a[i]&gt;a[j]){
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i++;
            j--;
        }   
        else if(a[i]&lt;a[j]){
            i++;
            }
         }
      }
   }

   public static void main(String[] args){
      int[] a = {1,0,1,0,1,1,0,1,1,1};
      System.out.println("Array before segragation : ");
      for(int i=0; i&lt;a.length; i++){
         System.out.print(a[i]+" ");
      }
      System.out.println();
      doSegregation(a);
      System.out.println("Array after segragation : ");
      for(int i=0; i&lt;a.length; i++){
        System.out.print(a[i]+" ");
       }
    }
 }
With the premise that you're not allowed to use Arrays.sort It'd be more succinct to write your own sorting method.

All you really need to do is initialize an array of the size, count the 1s and set that many positions from the end to 1.

This takes advantage of the fact that integers in java are initialized to 0; the resulting solution becomes straightforward.

Try it yourself, then look at this implementation:

public class Segregate {
  public static void main(String[] args) {
    int[] a = {1, 0, 1, 0, 1, 1, 0, 1, 1, 1};
    System.out.println("Before segregation: " + join(a, ", "));
    System.out.println("After  segregation: " + join(segregate(a), ", "));
  }

  private static int[] segregate(int[] onesAndZeros) {
    int[] segregated = new int[onesAndZeros.length];
    int end = segregated.length - 1;
    for (int count = countOnes(onesAndZeros); count &gt; 0; count--) {
      segregated[end--] = 1;
    }
    return segregated;
  }

  // Obviously only works in this specific case.
  private static int countOnes(int[] onesAndZeros) {
    int count = 0;
    for (int n : onesAndZeros) {
      count += n;
    }
    return count;
  }

  private static String join(int[] nums, String delim) {
    StringBuilder joined = new StringBuilder();
    for (int num : nums) {
      joined.append(delim).append(num);
    }
    return joined.substring(delim.length());
  }
}


Output: