SORTING ARRAY OF 0 AND 1 WITH ONE LOOP

 /**  
 sorting Array with Single Loop   
 */  
 class one  
 {  
 public static void main(String[]args)  
 {  
      int a[]={1,0,0,0,1,0,1,0};   
      int j=0;  
      for(int i=0;i<a.length;i++)  
      {  
           if(a[i]==0)  
           {  
             a[i]=1;  
            a[j++]=0;                 
           }  
      }  
      for(int i=0;i<a.length;i++)  
           System.out.println(a[i]);  
 }  
 }  

Post a Comment

5 Comments

  1. Excellent......... Awesome...... Great work....Hope u r blog will popular very soon....

    ReplyDelete
  2. It doesn't work for array {1,1,1,1,0,0,0,0,1,1}.

    ReplyDelete
    Replies
    1. Why do you think .... this program won't work for your example.. anyhow for your reference see the below link
      http://3.bp.blogspot.com/-PdnliNPhmeA/UT4ODghkh6I/AAAAAAAAAGA/evZ0KXn24bI/s1600/Array.png

      Delete
    2. #include

      int main(int argc, char **argv)
      {

      int a[]={1,1,1,1,0,0,0,0,1,1};
      int j=0,i;
      for(i=0;i<10;i++)
      {
      if(a[i]==0)
      {
      a[i]=1;
      a[j++]=0;
      }
      }
      for(i=0;i<10;i++)
      printf("%d\n",a[i]);
      return 0;
      }

      Delete