#include <string.h>
#include <iostream>
using namespace std;
void swap(char &x, char &y)
{
char temp = y;
y = x;
x = temp;
}
int main()
{
char color[] = {'r', 'w', 'b', 'w', 'w', 'b', 'r', 'b', 'w', 'r', '\0'};
int wFlag = 0;
int bFlag = 0;
int rFlag = strlen(color) - 1;
for(int i=0; i<strlen(color); ++i)
cout<<color[i]<<" ";
cout<<endl;
while(wFlag <= rFlag)
{
if(color[wFlag] == 'w')//if the color is white, wFlag++
wFlag++;
else if(color[wFlag] == 'b') //swap and then wFlag++,bFlag++
{
swap(color[bFlag], color[wFlag]);
bFlag++;
wFlag++;
}
else //w and r swap and rFlag--
{
while(wFlag < rFlag && color[rFlag]=='r')
rFlag--;
swap(color[rFlag], color[wFlag]);
rFlag--;
}
}
for(int i=0; i<strlen(color); ++i)
cout<<color[i]<<" ";
cout<<endl;
return 1;
}
#include <iostream>
using namespace std;
void swap(char &x, char &y)
{
char temp = y;
y = x;
x = temp;
}
int main()
{
char color[] = {'r', 'w', 'b', 'w', 'w', 'b', 'r', 'b', 'w', 'r', '\0'};
int wFlag = 0;
int bFlag = 0;
int rFlag = strlen(color) - 1;
for(int i=0; i<strlen(color); ++i)
cout<<color[i]<<" ";
cout<<endl;
while(wFlag <= rFlag)
{
if(color[wFlag] == 'w')//if the color is white, wFlag++
wFlag++;
else if(color[wFlag] == 'b') //swap and then wFlag++,bFlag++
{
swap(color[bFlag], color[wFlag]);
bFlag++;
wFlag++;
}
else //w and r swap and rFlag--
{
while(wFlag < rFlag && color[rFlag]=='r')
rFlag--;
swap(color[rFlag], color[wFlag]);
rFlag--;
}
}
for(int i=0; i<strlen(color); ++i)
cout<<color[i]<<" ";
cout<<endl;
return 1;
}