OneCompiler

Sort an array of 0s, 1s and 2s without using any sorting algorithm

204
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace OneCompiler
{
	public class Program
	{
		public static void Main(string[] args)
		{
			int n = int.Parse(Console.ReadLine());
			
			int[] arr = Array.ConvertAll(Console.ReadLine().Split(" "), new Converter<string,int>(StringToInteger));
			
			Sort012(arr);
			
			for (int i = 0; i < n; i++)
			{
			  Console.Write(arr[i] + " ");
			}
		}
		
		private static int StringToInteger(string s)
		{
		  return int.Parse(s);
		}
		
		private static void Sort012(int[] arr)
		{
		  int n = arr.Length;
		  
		  if (n == 0 || n == 1)
		  {
		    return;
		  }
		  
		  var zeroes = 0;
		  var ones = 0;
		  var twos = 0;
		  
		  for (int i = 0; i < n; i++)
		  {
		    if (arr[i] == 0)
		    {
		      zeroes++;
		    }
		    else if (arr[i] == 1)
		    {
		      ones++;
		    }
		  }
		  
		  twos = n - (zeroes + ones);
		  
		  for (int i = 0; i < n; i++)
		  {
		    if (i < zeroes)
		    {
		      arr[i] = 0;
		    }
		    else if (i >= zeroes && i < (zeroes + ones))
		    {
		      arr[i] = 1;
		    }
		    else
		    {
		      arr[i] = 2;
		    }
		  }
		}
	}
}