Advertisement
Guest User

Ved

a guest
May 26th, 2008
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. public class OddEvenArraySimplified
  2. {
  3.     public static void main(String args[])
  4.     {    
  5.         int oddInd=0;
  6.         int evenInd=1;
  7.         char[] str = "01101010111001011".toCharArray();
  8.         int strLen = str.length-1;
  9.         while(true)
  10.         {
  11.           while(oddInd<strLen && str[oddInd]=='1')
  12.           {
  13.               oddInd+=2;//here
  14.            }  
  15.           if(oddInd>=strLen)
  16.                  break;
  17.            while(evenInd<strLen &&str[evenInd]=='0')
  18.            {
  19.               evenInd+=2;//here
  20.            }  
  21.            if(evenInd>=strLen)
  22.              break;
  23.          
  24.             swap(str, evenInd, oddInd);
  25.          }
  26.           for(int i = 0 ;i< strLen; i++)
  27.               System.out.print(str[i] + ",");
  28.  
  29.         }
  30.     private static void swap(char[] str, int a, int b)
  31.     {
  32.         char tmp = str[a];
  33.         str[a] = str[b];
  34.         str[b]= tmp;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement