I do algorithm is very, very slowly, because to do probably 1 million times (1 million pixels), but the use of Array. Sort soon,
But the teacher not to use Array. Sort

Because the teacher said Array sort made useless, as long as we know, 41
Turn to the great god

What kind of algorithm is suitable for this kind of situation?