I tried to implement a recursive merge-sort, but I get a stack overflow error.
public class MergeSort {
static int input[],mid;
public static void mergeSort(int input[],int start,int end ){
if(start>end){
return;
}
// dividing input array into two equal parts
mid=(start+end)/2;
mergeSort(input,start,mid);
mergeSort(input,mid+1,end);
merge(input,start,mid,end);
}
public static void merge(int input[],int start,int mid,int end )
{
int l[]=new int[mid-start+1];
int r[]=new int[end-mid];
for(int i=1;i<end-start+1;i++){
l[i]=input[start+i-1];
}
for(int j=1;j<end-mid;j++){
r[j]=input[mid+j];
}
l[end-start+2]='\u221e';// ASCII vlaue of infinity at the end of left array
r[end-mid+1]='\u221e';//ASCII vlaue of infinity at the end of right array
int i=1,j=1;
for(int k=start;k<end;k++){
if(l[i]<=r[j]){
input[k]=l[i];
i=i+1;
}
else{
input[k]=r[j];
j=j+1;
}
}
printcommand at the top of each method to display the input arguments. That much will tell you a lot about the recursion progression.