题目:有n个蓝白红的球,乱序排放,现在要求以红白蓝的顺序排放;
红色用0表示,蓝色用1表示,白色用2表示
void DutchSort(int data[],int n)
{
int begin,centre,end; // 三个指针,前,中,后
begin = centre =0;
end = n-1;
while(centre <= end)
{
if(data[centre] == 0)
{
swap(data[centre],data[begin]);
centre++;
begin++;
}
else if(data[centre] == 1)
{
centre++;
}
else
{
swap(data[centre],data[end]);
end--;
}
}
}