A box contains Red and Green balls... .....
Microsoft Interview Questions and Answers
(Continued from previous question...)
278. A box contains Red and Green balls... .....
Question:
A box contains Red and Green balls...now find the algorithm which arranges all Red balls onside and then all Gren balls
maybe an answer:
arrangeBalls(int []balls, int n){ // n length
if(n<0) return;
int i=0; j=0;
while(i<n && j
j= i+1;
if(balls[i].color ! = balls[i+1].color){
j = j+1;
while(balls[i+1].color != balls[j].color && j
j++;
}
if(j<n){
char temp = new char();
temp = balls[i+1].color;
balls[i+1].color = balls[j].color;
balls[j].color = temp;
delete(temp);
}
}
i++;
}
}
(Continued on next question...)
Other Interview Questions
|