#define N 100 //size of the array to sort
unsigned char ArrayToSort[N]; //the actual array
unsigned char Counters[3] = {0, 0, 0}; //another array to store the number of 1's, 2's and 3's - its not used to copy the first array's data over so i assume it's ok ?
void Sort(void)
{
int a;
int b;
for (a = 0; a < N; a++)
{
if (ArrayToSort[a] & 0xFC) //and value with b'11111100' - if result is not 0 then there is a value other then 1,2 or 3 in the array
{
//Invalid value, other then 1, 2 or 3 in our array, handle error here.
}
Counters[ArrayToSort[a]]++; //add one to the counter of the corresponding value
}
a = 0;
b = 0;
while (b < 3)
{
while (Counters[b])
{
ArrayToSort[a] = b + 1;
Counters[b]--;
a++;
}
b++;
}
}