Я продолжаю получать
Исключение в потоке "основной" MergeSorter.java:14),
MergeSorter.sort (MergeSorter.java:14), MergeSorter.sort (MergeSorter.java:14)
Не уверен, как исправить.
Также хотите конвертировать в общий потом.
public class MergeSorter {
////change back to generic later
///array is item
public static void sort(int[] array, int begIndx, int endIndx) {
if (array == null) {
throw new IllegalArgumentException("Item is null.");
}
if(begIndx < endIndx) {
int midIndx = (int) Math.floor((begIndx + endIndx)/2);
sort(array, begIndx, midIndx);
sort(array, midIndx + 1, endIndx);
merge(array, begIndx, midIndx, endIndx);
}
}
//Takes to sorted arrays and merges them together
///Change type of array to generic later
public static void merge(int[] array, int begIndx, int midIndx, int endIndx) {
int sizeOfLeft = midIndx - begIndx + 1;
int sizeOfRight = endIndx - midIndx;
///change to generic later
int[] leftArr = new int[sizeOfLeft + 1];
int[] rightArr = new int[sizeOfRight + 1];
//removing equal sign from loop does nothing
for(int i = 1; i <= sizeOfLeft; i++) {
leftArr[i] = array[begIndx + i - 1];
}
for( int j = 1; j <= sizeOfRight; j++) {
rightArr[j] = array[midIndx + j];
}
leftArr[sizeOfLeft + 1] = Integer.MAX_VALUE;
rightArr[sizeOfRight + 1] = Integer.MAX_VALUE;
int i = 1;
int j = 1;
for(int k = begIndx; k < endIndx; k++) {
//use comparable here
if(leftArr[i] <= rightArr[j]) {
array[k] = leftArr[i];
i = i + 1;
}else {
///just replaces it so don't use comparable
array[k] = rightArr[j];
j = j + 1;
}
}
}
}