title: How to implement merge sort without recursion
body: <p>The arrays are always the size of powers of 2, and it can't be recursive.</p>
  
  <p>If I've made any errors in asking my question this is my first time asking a question. Bear with me please.</p>
  
  <p>The plan is to place markers in powers of two then loop through and place the ordered numbers into an array then putting them back in the array. Then should be placed into the original array in the groups that they were placed split into. Gradually getting bigger until the whole array is sorted.</p>
  
  <pre><code>public static void MergeSortNonRec(long[] a) {
      //======================
      //FILL IN YOUR CODE HERE
      //======================    
  
      System.out.println(a.length);  // print statement
      if (a == null) 
          return;
      int subArray1 = 0;
      int subArray2 = 1;
      int increment = 0;
  
      int swapCounter = 0;
  
      for (int i = 1; i &lt;= a.length; i *= 2) {
          // this loop determines the current size of the sub array
          increment = i;
          subArray1 = 0;
          subArray2 = 0;
          swapCounter = 0;
  
          while (subArray2 &lt;= a.length) {
              // this will loop until we reach the end of the array
  
              subArray2 += increment;
  
              long[] spareArray = new long[2 * i];
              swapCounter = 0;
              while (swapCounter &lt;= spareArray.length) {
                  System.out.println(subArray1 + " " + subArray2); // print statement
                  if (a[subArray1] &lt; a[subArray2]) {
                      spareArray[swapCounter] = a[subArray1];
                      swapCounter++;
                      subArray1++;
                  } else
                  if (a[subArray1] &gt; a[subArray2]) {
                      spareArray[swapCounter] = a[subArray2];
                      swapCounter++;
                      subArray2++;
                  } else
                  if (a[subArray1] == a[subArray2]) {
                      spareArray[swapCounter] = a[subArray1];
                      swapCounter++;
                      subArray1++;
                      System.out.println(swapCounter + " " + subArray1); // print statement
                      spareArray[swapCounter] = a[subArray1];
                      swapCounter++;
                      subArray2++;
                  }
              } // this creates an array of the ordered elements 
  
              while (swapCounter == spareArray.length) {
                  subArray1 -= swapCounter / 2;
                  subArray2 -= swapCounter / 2;
                  // now the sub array pointers are back to the values they started at
                  int spareArrayPointer = 0;
                  for (; subArray1 &lt;= subArray2; subArray1++) {
                      a[subArray1] = spareArray[spareArrayPointer];
                      spareArrayPointer++;
                  } // this places the values in the spare array into the original array
                  subArray1 -= spareArrayPointer;
                  spareArrayPointer = 0;
                  // takes the first pointer back to where it started
              }
              subArray1 += increment;
              subArray2 += increment + increment;
          } // end subArray2 &lt;= a.length loop
      } // end 2*i loop
  } //MergeSortNonRec()
  </code></pre>