title: sorting 0 and 1 in Java
body: <p>Unable to figure out the problem in the code
  It says Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
  I have gone through it a lot of times,still unable to figure whats wrong</p>
  
  <p>Note: Trying to sort 0, 1 in an array</p>
  
  <pre><code>public class javaarray {
  
      public static void main(String[] args) {
  
          int[] arr = new int[]{1, 0, 0, 1, 1, 0}; 
          int left = 0, right =arr.length-1;
          while(left&lt;right) {
              while(arr[left]==0 &amp;&amp; left&lt;right) {
                  left++;
              }
              while(arr[right]==1 &amp;&amp; left&lt;right){
                  right++;
              }
              if(left&lt;right) {
                  arr[left]=0;
                  arr[right]=1;
                  left++;
                  right--;
                }  
             }
  
             for(int i=0; i&lt;arr.length;i++) {
                 System.out.print(arr[i]) 
             }
         }
      }
  
  }
  </code></pre>