bugbug
BAN USERJust maintain count of R, G, and B. And then overwrite whole array. Some thing like below
void Sort(char *arr, int len)
{
int count[3] = {0, 0, 0};
char chars[3] = {'R', 'G', 'B'};
for (int i = 0; i < len ++ len)
{
if (arr[i] == 'R') ++count[0];
else if (arr[i] == 'G') ++count[1];
else if (arr[i] == 'B') ++count[2];
// in else print error and return
}
int k = 0;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < count[i]; ++j)
{
arr[k++] = chars[i];
}
}
}
typedef Node* NodePtr;
void FindSuccessor(NodePtr root, int k, NodePtr& successor)
{
if (root == NULL)
{
return;
}
if (root->data <= k)
{
FindSuccessor(root->right, k, successor);
}
if (successor == NULL || root->data < successor->data )
{
successor = root;
}
FindSuccessor(root->left, k, successor);
}
- bugbug May 15, 2012