0

I am coding the non-recursive merge sort algorithm in Java I have to make sure if this method works as a non recursive as well as the space complexity should be O(N)

Instruction I got: You can use O(N) space (in addition to the input array) and your algorithm should have the same running time as recursive merge sort.

here's my code.

I want to make sure the recursiveness as well as the O(N) space If there's a better way, please let me know.

private static void merge(Integer[] a, Integer[] tmpArray, int leftPos, int rightPos, int rightEnd) {
       int leftEnd = rightPos - 1;
       int tmpPos = leftPos;
       int numElements = rightEnd - leftPos + 1;

       // Main loop
       while(leftPos <= leftEnd && rightPos <= rightEnd) {
           if( a[leftPos] <= a[rightPos ]) {
               tmpArray[tmpPos++] = a[leftPos++];
           } else {
               tmpArray[tmpPos++] = a[rightPos++];
           }
       }

       while( leftPos <= leftEnd ) {   // Copy rest of first half
           tmpArray[tmpPos++] = a[leftPos++];
       }

       while( rightPos <= rightEnd ) { // Copy rest of right half
           tmpArray[tmpPos++] = a[rightPos++];
       }

       // Copy tmpArray back
       for( int i = 0; i < numElements; i++, rightEnd-- ) {
           a[rightEnd] = tmpArray[rightEnd];
       }
   }

   public static void mergeSortB(Integer[] inputArray) {

     Integer[] tempArray = new Integer[inputArray.length];

     for(int i = 1; i<inputArray.length; i=i*2) {
       for(int j=i; j<inputArray.length; j=j+i*2) {
         int k = j+i-1;
         if(inputArray.length<j + i) {
           k = inputArray.length -1;
         }
         //call the merge method(non recursive)
        merge(inputArray, tempArray, j-i,j, k);
       }
     }

   }
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
Daniel Park
  • 39
  • 1
  • 7
  • 1
    What do you mean O(n)? Merge sort has time complexity of O(NlogN). – xiaofeng.li Dec 06 '16 at 23:03
  • I got an instruction from the professor You can use O(N) space (in addition to the input array) and your algorithm should have the same running time as recursive merge sort. I am not sure what it meant. I thought the method should be bigO(N) but maybe not. – Daniel Park Dec 06 '16 at 23:09
  • You can try non comparison based algorithms: https://www.cs.bgu.ac.il/~ds132/wiki.files/ds132_ps11.pdf – Veneet Reddy Dec 06 '16 at 23:10
  • 1
    The instructions mean that the method should have time complexity O(n*log(n)) and space complexity O(n) – that other guy Dec 06 '16 at 23:12
  • Does my code meet that? – Daniel Park Dec 06 '16 at 23:13
  • Yes, it appears so. The only size-dependent data you allocate is the temp array of size N so that's trivially O(n) space. Then you linearly go through each non-overlapping sublist of size X where X doubles every time (so log2(n) times), giving O(n*log(n)) time. – that other guy Dec 06 '16 at 23:36
  • Thank you!! and as the method calls merge instead of mergeSortB, it is non recursvie as well right? – Daniel Park Dec 06 '16 at 23:37

1 Answers1

-1

Your code looks okay. Although, you can create a test (and debug if necessary) to make sure your code is working. But Merge Sort has Big(O)== NlogN, not N.

Saber Alex
  • 1,544
  • 3
  • 24
  • 39
  • I added an instruction that I have from the professor. You can use O(N) space (in addition to the input array) and your algorithm should have the same running time as recursive merge sort. I wonder it means that the loop should be bigO(N) – Daniel Park Dec 06 '16 at 23:04
  • The debugger tool normally has a way to show the variable in your stack, – Saber Alex Dec 06 '16 at 23:04