Sort R,G,B Characters in an array

Manojjupally
Posted by Manojjupally under C# category on | Points: 40 | Views : 1544
Sort R,G,B in array

public static void SortRGB()
{
string[] a = { "R", "G", "B", "R", "G", "B", "R", "R", "B", "G", "B", "G" };

int r = 0;
int g = 0;
int b = a.Length-1;
string temp;
for (int j = 0; j < a.Length; j++)
{
if (a[j] == "R")
{
temp = a[r];
a[r] = a[j];
a[j] = temp;
r++;
}
else if (a[j] == "B" && j<b)
{
temp = a[b];
a[b] = a[j];
a[j] = temp;
if (a[j] == "B")
j--;
b--;
}
}

for (int j = 0; j < a.Length; j++)
{
Console.Write(a[j]);
}

}

Comments or Responses

Posted by: T.Saravanan on: 1/10/2013 Level:Silver | Status: [Member] [MVP] | Points: 10
Kindly use any other variable instead of 'i' in for loop. Because in DNF Square brace of 'i' consider as 'Italic' format.

Login to post response