title: Merging multiple sorted arrays in C
body: <p>Trying to merge 8 pre-sorted arrays. I'm fairly new to C, but this is what I've come up with so far. Needless to say it doesn't work. What I can't figure out is why. I've based it off the C++ mergesort implementation <a href="http://www.algolist.net/Algorithms/Merge/Sorted_arrays">here</a> and tried to extend it to 8 dimensions and simplify it a bit, but for some reason it tends to give me the elements of the first array, followed by 34, 30 then it repeats 23 until the end. It doesn't even pick up that 21 is the smallest value on the first iteration. </p>
  
  <pre><code>int test1[5] = {23, 24, 25, 33, 51};
  int test2[5] = {21, 34, 44, 50, 62};
  int test3[5] = {34, 36, 41, 44, 46};
  int test4[5] = {30, 31, 32, 35, 40};
  int test5[5] = {54, 56, 57, 58, 60};
  int test6[5] = {31, 33, 36, 51, 52};
  int test7[5] = {44, 46, 76, 78, 79};
  int test8[5] = {23, 33, 43, 54, 63};
  
  int output[40];
  int t1, t2, t3, t4, t5, t6, t7, t8;
  t1 = 0;
  t2 = 0;
  t3 = 0;
  t4 = 0;
  t5 = 0;
  t6 = 0;
  t7 = 0;
  t8 = 0;
  int p = 0;
  int temp1;
  int temp2;
  
  while(p &lt; 40) {
      if (t1 &lt; 5) {
          temp1 = 1;
          temp2 = test1[t1];
      }else if (test2[t2] &lt;= test1[t1] &amp;&amp; t2 &lt; 5) {
          temp1 = 2;
          temp2 = test2[t2];
      }else if (test3[t3] &lt;= temp2 &amp;&amp; t3 &lt; 5) {
          temp1 = 3;
          temp2 = test3[t3];
      }else if (test4[t4] &lt;= temp2 &amp;&amp; t4 &lt; 5) {
          temp1 = 4;
          temp2 = test4[t4];
      }else if (test5[t5] &lt;= temp2 &amp;&amp; t5 &lt; 5) {
          temp1 = 5;
          temp2 = test5[t5];
      }else if (test6[t6] &lt;= temp2 &amp;&amp; t6 &lt; 5) {
          temp1 = 6;
          temp2 = test6[t6];
      }else if (test7[t7] &lt;= temp2 &amp;&amp; t7 &lt; 5) {
          temp1 = 7;
          temp2 = test7[t7];
      }else if (test8[t8] &lt;= temp2 &amp;&amp; t8 &lt; 5) {
          temp1 = 8;
          temp2 = test8[t8];
      }
      switch(temp1) {
          case 1:
              output[p] = temp2;
              t1++;
              break;
          case 2:
              output[p] = temp2;
              t2++;
              break;
          case 3:
              output[p] = temp2;
              t3++;
              break;
          case 4:
              output[p] = temp2;
              t4++;
              break;
          case 5:
              output[p] = temp2;
              t5++;
              break;
          case 6:
              output[p] = temp2;
              t6++;
              break;
          case 7:
              output[p] = temp2;
              t7++;
              break;
          case 8:
              output[p] = temp2;
              t8++;
              break;
      }
      printf("%d\n",  output[p]);
      p++;
  }
  </code></pre>
  
  <p>Thanks for any help you can offer.</p>