title: Count Sort in C - problem with output and count_array
body: <p>I am trying to implement a count sort algorithm in C for uni. The task was pretty easy because we were given pseudocode to translate into c-code. But somehow my <code>count_array</code> and <code>output_array</code> go crazy. But i don't see where i am writing in indices i am not supposed to or where there is a memory fault.</p>
  
  <p>Edit: The console gives out the following </p>
  
  <pre><code>Unsortiertes Array:(print input_array) 90 38 42 34 8 0 77 1 84 5 25 72 44 42 90 63 23
  
  (print count_array) -363607072 32766 233318081 1 1 0 0 0 -363606696 32766 -363606200 32766 1805418521 32767 13 0 0
  
  Sortiertes Array: print output_array) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  </code></pre>
  
  <p>What am i doing wrong? i think it is the <code>output_array[k] = j;</code> in <code>count_sort_write_output_array</code> but when i look up other counting sort algos on the internet thats how its supposed to be written, or not?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "arrayio.h"
  
  int MAX_LAENGE = 1000;
  int MAX_VALUE = 100;
  int j, i, k;
  
  
  void count_sort_calculate_counts(int input_array[], int len, int count_array[]) {
      for (j = 0; j &lt; len; j++) {
          count_array[input_array[j]] = count_array[input_array[j]] + 1;
      }
  }
  
  void count_sort_write_output_array(int output_array[], int len, int count_array[]) {
      k = 0;
      for (j = 0; j &lt; len; j++) {
          for (i = 0; i &lt; count_array[j]; i++) {
              output_array[k] = j;
              k = k + 1;
          }
      }
  }
  
  
  
  int main(int argc, char *argv[]) {
  
      if (argc &lt; 2){
          printf("Aufruf: %s &lt;Dateiname&gt;\n", argv[0]);
          printf("Beispiel: %s zahlen.txt\n", argv[0]);
          exit(1);
      }
  
      char *filename = argv[1];
  
      int input_array[MAX_LAENGE];
      int len = read_array_from_file(input_array, MAX_LAENGE, filename);
  
      printf("Unsortiertes Array:");
      print_array(input_array, len);
  
       int count_array[MAX_LAENGE];
       // print_array(count_array, len);
       int output_array[MAX_LAENGE];
       count_sort_calculate_counts(input_array, len, count_array);
       count_sort_write_output_array(output_array, len, count_array);
  
      printf("Sortiertes Array:");
  
      print_array(output_array, len);
  
      return 0;
  }
  </code></pre>