public class mergesort {
public static int[] mergesort (int[] input) {
int length = input.length;
if (length <= 1) {
return input;
}
int median = length/2;
int[] left = new int[median];
int[] right = new int[length-median];
for (int x = 0; x< median; x++) {
left[x] = input[x];
}
for (int y = median; y < length; y++) {
right[y-median] = input[y];
}
mergesort(left);
mergesort(right);
merge(left, right, input);
return input;
}
public static int[] merge (int[] left, int[] right, int[] input) {
int a = 0;
int b = 0;
int c = 0;
int leftlength = left.length;
int rightlength = right.length;
while (a<leftlength && b<rightlength) {
if (left[a] < right[b]) {
input[c] = left[a];
a++;
} else {
input[c] = right[b];
b++;
}
c++;
}
return input;
}
public static void main(String[] args) {
int[] inputArr = {45,23,11,89,77,98,4,28,65,43};
mergesort mms = new mergesort();
inputArr = mms.mergesort(inputArr);
for(int i = 0; i < inputArr.length; i ++){
System.out.print(inputArr[i]);
System.out.print(" ");
}
}
This above is my attempt at the merge-sort algorithm. The last code block with main(String[] args) is a test run to see if my algorithm works.
The code still prints out an array (before, it just threw off an error). However, while the algorithm should be printing
4, 11, 23, 28, 43, 45, 65, 77, 89,and 98,
the code prints:
4 4 11 23 23 28 65 43 65 43,
which is obviously not the intended result.
How can I fix my code?
It's my first post here, so please let me know if I'm not allowed to post these kinds of material, etc.
Edit: This is (surprisingly) an edited version of the original code. Before, the code threw off an error, but now at least it prints something. Manipulating small parts of the code (ex. changing median to median+1, etc) didn't work out, and I didn't think it would be very significant to post a plethora of trial-and-failure results that may be deemed insignificant to some.
And yes, I honestly didn't know where to go from here. I asked my friend to review the code, and he used the debugger to check the code, but there weren't any meaningful discoveries, thus this post.
while (a<leftlength && b<rightlength)may terminate early, even there are elements still available in left or right array. 2. variable c will not be initialized with 0.