The flaw is that your merge doesn't copy symbols from right part if left[i1] - right[i2] > 0.
This works:
public static void merge(char[] string, char[] left, char[] right) {
int i1 = 0;
int i2 = 0;
for (int i = 0; i < string.length; i++) {
if (i1 < left.length && i2 < right.length) {
if (left[i1] - right[i2] <= 0) {
string[i] = left[i1];
i1++;
} else {
string[i] = right[i2];
i2++;
}
} else if (i2 >= right.length) {
string[i] = left[i1];
i1++;
} else {
string[i] = right[i2];
i2++;
}
}
}
You have three cases:
- There are symbols available in both parts. Take one with the least value.
- There are symbols available only in the left part. Copy them.
- There are symbols available only in the right part. Copy them.