I have array of colors:
String s[]=new String[]{"red","black..............};
The only possible colors are red
and black
. I need an algorithm which partitions the array such that all red
come before all black
. How can I compare/exchange them to accomplish that?
i have tried this
public class Partition{
public static void main(String[]args){
String temp="";
String s[]=new String[]{"red","black","red","black","black","red","red","black","red"};
int i=0;
int j=s.length;
while (i<j){
do{
i++;
} while (s[i]!="black");
do{
j--;
} while (s[i]!="red");
if (i>j) break;
if (i<j){
exch( s,i,j);
}
}
for (int k=0;k<s.length;k++){
System.out.println(s[k]);
}
}
public static void exch ( String s[],int i,int j){
String m=s[i];
s[i]=s[j];
s[j]=s;
}
}
but it does not show me result