title: Split C array into n equal parts
body: <p>I am trying to split an array into n equal parts by calculating start and end indices. The address of the start and end elements will be passed into a function that will sort these arrays. For example, if arraySize = 1000, and n=2, the indices will be 0, 499, 999. So far I have the below code but for odd n, it is splitting it into more than n arrays. Another way I thought of doing this is by running through the loop n times, but I'm not sure where to start.</p>
  
  <pre><code>  int chunkSize = arraySize / numThreads;
    for (int start = 0; start &lt; arraySize; start += chunkSize) {
        int end = start + chunkSize - 1;
        if (end &gt; arraySize - 1) {
            end = arraySize - 1;
        }
  
        InsertionSort(&amp;array[start], end - start + 1);
    }
  </code></pre>
  
  <p><strong>EDIT:</strong> Here's something else I came up with. It seems to be working, but I need to do some more thorough testing. I've drawn this out multiple times and traced it by hand. Hopefully, there aren't any edge cases that will fail. I am already restricting n >= arraySize.</p>
  
  <pre><code>int chunkSize = arraySize / numThreads;
  for (int i = 0; i &lt; numThreads; i++) {
      int start = i * chunkSize;
      int end = start + chunkSize - 1;
      if (i == numThreads - 1) {
          end = arraySize - 1;
      }
  
      for (int i = start; i &lt;= end; i++) {
          printf("%d ", array[i]);
      }
          printf("\n");
  }
  </code></pre>