69,381
社区成员
发帖
与我相关
我的任务
分享
int dutch_flag(int color[], int n )
{
int white = 0 ;
int blue = 0 ;
int red = n - 1 ;
int t = 0 ;
while(white <= red) /* is there are more token*/
{
if(color[white] == WHITE) /* is a white */
white ++ ; /* Yes just inc. white ptr */
else if(color[white] == BLUE ) /* is a blue */
{
swap(color[blue],color[white]); t++; /* Yes ,swap the last w */
blue ++; /* and the last b and inc. */
white ++ ;
}
else /* or is it a red ,skip */
{
while(white < red && color[red] == RED)
red --; /* red token and */
swap(color[red],color[white]); t ++; /* swap with the last w */
red -- ; /* dec red ptr */
}
}
return t ;
}