My issue
stack overflow error
My code
public static void mergeSort(int[] arr, int left, int right) {
// Write your code here
if(left>=right)
{
return;
}
int mid=left+(right-left)/2;
mergeSort(arr,left,mid);
mergeSort(arr,mid,right);
merge(arr,left,mid,right);
}
public static void merge(int arr[],int left,int mid,int right)
{
int temp[]=new int[right-left+1];
int i=left;
int j=mid+1;
int k=0;
while(i<=left&&j<=right)
{
if(arr[i]<arr[j])
{
temp[k]=arr[i];
i++;
}
else{
temp[k]=arr[j];
j++;
}
k++;
}
while(i<=left)
{
temp[k++]=arr[i++];
}
while(j<=right)
{
temp[k++]=arr[j++];
}
//copy temp array to original array
for(k=0,i=left;k<temp.length;k++,i++)
{
arr[i]=temp[k];
}
}
Learning course: Design and analysis of Algorithms
Problem Link: https://www.codechef.com/learn/course/abesit-daa/ABESITDA21/problems/SESO43