Easy to understand solution in java ->


#1

public class Solution {
public void sortColors(ArrayList a) {
int n = a.size() ;
int index = 0 ;
for( int i = 0 ; i < 3 ; i++ )
{
int j = 0 ;
while( j < n && index < n )
{
if( a.get(j) == i )
{
Collections.swap( a , j , index ) ;
j++ ;
index++ ;
}
else
{
j++ ;
}
}

  } 

}
}