1 public class Solution { 2 public void sortColors(int[] A) { 3 int len=A.length; 4 int beg=0; 5 int end=len-1; 6 int cur=0; 7 while(cur<=end){ 8 if(A[cur]==0) 9 { 10 swap(A,cur,beg); 11 beg++; 12 cur++; 13 14 15 } 16 else if(A[cur]==1) 17 { 18 cur++; 19 20 } 21 else 22 { 23 swap(A,cur,end); 24 end--; 25 26 27 } 28 29 30 } 31 } 32 public void swap(int[] A,int i,int j) 33 { 34 int temp=A[i]; 35 A[i]=A[j]; 36 A[j]=temp; 37 38 39 40 } 41 42 43 44 }