C语言三色旗有关问题(考试最后一道编程题)

C语言三色旗问题(考试最后一道编程题)
三色旗问题,一个字符串Color,其中每个元素值为‘R’‘W’‘B’三者之一,实现把数组中元素重新排列,所有蓝色在前,白色其后,红最后。
编写这么一个程序。
网上找到以下算法:

其實要實行這演算法抓住這幾個原則: 
在 0 到 b - 1 之間放藍色,b 到 w - 1 放白色,r + 1 到 n - 1 放紅色,而 w 到 r 之間元素未曾處理... 
每一次都處理 w 所在元素,有三種情形: 
1. 如果 w 所在位置是白色:那就把 w 加上 1 
2. 如果 w 所在元素是藍色:把 b 與 w 所在元素對調,b、w 各加 1,維持上圖 
3. 如果 w 所在元素是紅色:就把 w 和 r 元素互換,但是 r 減 1,w 不需減 1

看不太懂,这个程序应该怎么写,请高人指点

------解决方案--------------------

------解决方案--------------------
#include "stdio.h"
#include "string.h"
#include "conio.h"
#define N 100

void main()
{
char color[N],temp;
int i,j;
printf("input color R or W or B:");
gets(color);

for(i=1;i<N;i++)
{
for(j=N-1;j>=i;j--)
{
if(color[j]!='\0')
{
if(color[j-1]>color[j])
{
temp=color[j-1];
color[j-1]=color[j];
color[j]=temp;
}
}
}
}
for(i=0;i<N;i++)
{
if(color[i]=='W')
{
color[i]='R';
continue;
}
if(color[i]=='R')
{
color[i]='W';
}
}
for(i=0;i<N;i++)
{
if(color[i]=='B'||color[i]=='W'||color[i]=='R')
{
printf("%s",color[i]);
}
}
getch();
}

------解决方案--------------------
char str[]="RWRRWBBRWBWR";
int len=strlen(str);
int i=0,j=0;
char t;
for(i=0;i<=len;i++)
for(j=0;j<len-i;j++)
{
if (str[j+1] == '\0')
continue;
if ((str[j] == 'R') && (str[j+1] != 'R'))

t=str[j]; 
str[j]=str[j+1];
str[j+1]=t;
continue;
}
else if ((str[j] == 'W') && (str[j+1] == 'B'))
{
t=str[j]; 
str[j]=str[j+1];
str[j+1]=t;
continue;
}
else if ((str[j] == 'B'))
continue;
else if (str[j] == str[j+1])
continue;
}

printf("%s\n",str);
return 0;[/code]
------解决方案--------------------
C/C++ code

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 ;
}

------解决方案--------------------
5楼的人真无聊,这道题是Dijkstra提出来的,要求就是不能用额外空间 以及交换次数最少

另补充一下
#define BLUE 3
#define WHITE 2
#define RED 1

int i , j , k ,n,t;