I was practicing to do merge sort. This is the problem i got, it shows array index out of bounds, i checked the for loop and modified with < which was before <=, i dont know how to resolve
public class MergeSortProblem {
public static void merge(int[] arr, int[] res, int low, int mid, int high) {
int i = low, k= low, j=mid+1;
while(i<=mid && j<=high) {
if(arr[i] <= arr[j]) {
res[k++] = arr[i++];
}
else {
res[k++] = arr[j++];
}
}
while(i<=mid) {
res[k++] = arr[i++];
}
while(j<=high) {
res[k++] = arr[j++];
}
for(i =low; i<=high; i++ ) {
arr[i] = res[k];
}
}
public static void divideArray(int[] arr, int[] res, int low, int high) {
while(low == high) {
return;
}
int mid = (low + high)/2 ;
divideArray(arr, res, low,mid);
divideArray(arr,res, mid+1, high);
merge(arr,res,low,mid,high);
}
public static void main(String[] args) {
int[] array = {6,10,5,7,6,8,3,80};
int size = array.length;
int start = 0;
int[] result = {};
divideArray(array,result,start,size);
}
}
why am i getting array index out of bounds exception?