title: Optimize counting sort?
body: <p>Given that the input will be N numbers from 0 to N (with duplicates) how I can optimize the code bellow for both small and big arrays:</p>
  
  <pre><code>void countingsort(int* input, int array_size)
  {
      int max_element = array_size;//because no number will be &gt; N
  
      int *CountArr = new int[max_element+1]();
  
      for (int i = 0; i &lt; array_size; i++)
          CountArr[input[i]]++;
  
      for (int j = 0, outputindex = 0; j &lt;= max_element; j++)
          while (CountArr[j]--)
              input[outputindex++] = j;
  
      delete []CountArr;
  }
  </code></pre>
  
  <p>Having a stable sort is not a requirement.</p>
  
  <p><strong>edit:</strong> In case it's not clear, I am talking about optimizing the algorithm.</p>