java - Debugging Mergesort Implementation -


OK, I know this question is not showing a research effort, but I have this code several times and many times I'm not running I did not know that I was doing wrong I know that there are examples of many mergeways implementation but I wanted to make it my way. Any help is appreciated, thanks.

  import java.util.Scanner; Public class MergeSort {public static int [] mergeSort (int [] arr) {if (arr.length & gt; 1) {int [] arr1 = splitLeft (arr); Int [] arr2 = splitRight (arr); Arr1 = Merger (arr1); Arr2 = mergers (arr2); Return merge (arr1, arr2); } And arrival to return; } Public static int [] splitLeft (int [] arr) {int middle = arr.length / 2; Int [] newarr = new int [middle]; For (int i = 0; i & lt; medium; i ++) new arb [ii] = ARR [i]; Return new arb; } Public fixed int [] splitRight (int [] arr) {int middle = arr.length / 2; Int [] newarr = new int [arr.length - middle]; For (int i = 0; i + middle    

I think the problem is in your splitRight Is the ceremony Consider this code:

 for  (int i = middle; i & lt; arr.length; i ++) newarr [i] = arr [i];   

tries to copy the i th element from arr to i th < NewR , but this is incorrect. For example, if there are ten elements of the array arr , then you 5 elements of arr If you want to copy the status of, element 6 of < newarr , etc. to position 1 code> AR

to fix this, Try doing something like:

  (int i = 0; i + middle   

Hope it helps!

Comments