title: Steps of counting sort in MPI: segmentation fault 11
body: <p>I have this code that implements the counting sort in C:</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;time.h&gt;
  
  int main(){
      //create an array of N random elements
      int N = 100;
      int my_array[N];
      srand(time(NULL));
      int i;
      for (i=0; i&lt;N; i++){
          my_array[i]=rand()%100 + 1;
      }
      //print the array 
      for (i=0; i&lt;N; i++){
          printf(&quot;%d &quot;, my_array[i]);
      } 
  
      clock_t start_time, end_time;
      double computation_time;
      start_time = clock();
  
      //define the minimum and the maximum as the first element of the array
      int min_array=my_array[0];
      int max_array=my_array[0];
  
      printf(&quot;\n--------------\n&quot;);
  
      //find the minimum and the maximum of the array
      for(i=0; i&lt;N; i++){
          if(my_array[i]&lt;min_array){
              min_array=my_array[i];
          }
          else if(my_array[i]&gt;max_array){
              max_array=my_array[i];
          }
      }
  
      //check if it worked
      printf(&quot;max_array %d\n&quot;, max_array);
      printf(&quot;min_array %d\n&quot;, min_array);
  
      //
      int range_array;
      range_array = max_array - min_array + 1;
      int count_array[range_array];
      for(i=0; i&lt;range_array;i++)
          count_array[i]=0;
  
      int j=0;
      
      for(int i=0; i&lt;N; i++){
          count_array[my_array[i]-min_array]=count_array[my_array[i]-min_array]+1;
      }
      
      int z = 0;
  
      for(i=min_array; i&lt;=max_array; i++){
          for(j=0; j&lt;count_array[i-min_array];j++)
              my_array[z++]=i;
              //z=z+1;
      }
  
      end_time = clock();
      computation_time = ((double) (end_time - start_time)) / CLOCKS_PER_SEC;
      printf(&quot;Time Taken: %lf \n&quot;, computation_time);
  
      for(i=0; i&lt;N; i++){
          printf(&quot;%d &quot;, my_array[i]);
      }
  
      printf(&quot;\n--------------\n&quot;);
      printf(&quot;Time Taken: %lf \n&quot;, computation_time);
  
  }
  </code></pre>
  <p>I have to implement this code using MPI.
  Basically, the first step of counting sort is to find the maximum and the minimum of an array. So I started trying to implement this search for max and min using MPI:</p>
  <pre><code>// First call MPI_Init
      MPI_Init(&amp;argc, &amp;argv);
      t1 = MPI_Wtime(); 
      // Get my rank
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
      // Get the number of ranks
      MPI_Comm_size(MPI_COMM_WORLD,&amp;n_ranks);
  
  
      //define the minimum and the maximum as the first element of the array
      int min_array=my_array[0];
      int max_array=my_array[0];
  
      printf(&quot;\n--------------\n&quot;);
  
      MPI_Reduce(my_array,&amp;min_array,N,MPI_INT,MPI_MIN,0,MPI_COMM_WORLD);
      MPI_Reduce(my_array,&amp;max_array,N,MPI_INT,MPI_MAX,0,MPI_COMM_WORLD);
  
      if (rank == 0){
          printf(&quot;Max_value is: %d\n&quot;, max_array);
          printf(&quot;Min_value is: %d\n&quot;, min_array);
      }
  </code></pre>
  <p>Inside the main, just after the creation of the array, I have written these lines of code in order to do this task of min and max. However, if I try to execute sometimes it compiles (but the search is wrong) and sometimes appears <code>Segmentation fault: 11</code></p>