Sorting a Binary Array with O(n) Complexity

I found the solution rather menial. But nevertheless, I still felt inspired to blog about it, since typically sorting algorithm are O(n^2) or O(n log n) which neither are ideal. The key here is that we are sorting binary values so it's safe to assume the resultant values in a discretized fashion.

Implementation

Basically you just count all the zeros and put it in the front and then you loop through the remaining values and put in 1s. If you wanted it in ascending order you would look for 1s initially instead of 0s.