title: Two-Way Insertion Sort not sorting
body: <p>This is supposed to be a Two-Way insertion sort, but it's not sorting. I'm also supposed to print out the number of assignments for sorting, but right now I just want it to sort. </p>
  
  <p>A separate output array of size <code>2n+1</code> is set aside. Initially <code>x[0]</code> is placed into the middle element of the array <code>n</code>. 
  Continue inserting elements until you need to insert between a pair of elements in the array.
  As before you need to make room for the new element by shifting elements. Unlike before,
  you can choose to shift all smaller elements one step to the left or all larger elements one step
  to the right since there is additional room on both sides of the array. The choice of which
  shift to perform depends on which would require shifting the smallest amount of elements.</p>
  
  <p>I can't find much on the internet about this sort except that no one uses it.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  void printArray(int arr[], int len) {
      for (int j = 0; j &lt; len; j++)
          printf("%d ", arr[j]);
  
      printf("\n");
  }
  
  int main() {
      FILE *in;
      int size_arr = 0;
      char ch;
  
      if ((in = fopen("data_a5.txt", "r")) == NULL) {
          printf("Error!");
          exit(1);
      }
      do {
          ch = fgetc(in);
          if (ch == '\n')
              size_arr++;
      } while (ch != EOF);
      rewind(in);
  
      int arr[size_arr];
      int sort_arr[2 * size_arr + 1];
  
      int n = 0;
      while (!feof(in)) {
          fscanf(in, "%d", &amp;arr[n]);
          n++;
      }
      fclose(in);
  
      for (n = 0; n &lt; 2 * size_arr; n++) {
          sort_arr[n] = 0;
      }
  
      sort_arr[size_arr] = arr[0];
  
      for (n = 1; n &lt; size_arr; n++) {
          int index = size_arr;
          if (arr[n] &lt;= sort_arr[size_arr]) {
              while (!(arr[n] &lt;= sort_arr[index]) &amp;&amp; sort_arr[index] != 0 &amp;&amp; index &gt;= 0) {
                  index--;
              }
          }
          if (arr[n] &gt; sort_arr[size_arr]) {
              while (!(arr[n] &lt;= sort_arr[index]) &amp;&amp; sort_arr[index] != 0 &amp;&amp; index &lt; 2 * size_arr) {
                  index++;
              }
          }
  
          if (sort_arr[index] == 0) {
              sort_arr[index] = arr[n];
          } else {
              int next_R, next_L = index;
              while (sort_arr[next_R] != 0 &amp;&amp; next_R &lt;= 2 * size_arr) {
                  next_R++;
              }
              while (sort_arr[next_L] != 0 &amp;&amp; next_L &gt;= 0) {
                  next_L--;
              }
              int R_move = next_R - index;
              int L_move = index - next_L;
              if (R_move &gt; L_move) {
                  while (L_move &lt;= index) {
                      sort_arr[L_move] = sort_arr[L_move + 1];
                      L_move++;
                  }
                  sort_arr[index] = arr[n];
              } else {
                  while (R_move &gt;= index) {
                      sort_arr[R_move] = sort_arr[R_move - 1];
                      R_move--;
                  }
                  sort_arr[index] = arr[n];
              }
          }
      }
      printArray(arr, size_arr);
      return 0;
  }
  </code></pre>