// main function
void mergeSort(int input[],int size){ //
if(size == 0 || size == 1) return; //base case
int m = size/2; //finding the mid
mergeSort(helper(input,0,m),m); //left array
mergeSort(helper(input,m,size),size-m); // right array
merge(input,0,m,size); // merging the both arrays
}
void merge(int *input,int start,int size1,int size) //merging array in increasing order
{
int a[size];
int i=start,j=size1,k=0;
while(i < size1 && j < size){
if(input[i] < input[j]){
a[k++] = input[i++];
}
else{
a[k++] = input[j++];
}
}
while(i < size1){
a[k++] = input[i++];
}
while(j < size){
a[k++] = input[j++];
}
for(int i=0;i<size;i++){
input[i] = a[i];
}
}
int* helper(int input[],int s,int e){// this function helps get the half array
int a[e-s];
for(int i=s,j=0;i<e;i++,j++){
a[j] = input[i];
}
for(int i=0;i<e-s;i++){
input[i] = a[i];
}
return input;
}
I am not able to debugg this code can anybody tell me why this is not right I divide the two half with the help of helper function and then pass it to the merge to get the output
Aucun commentaire:
Enregistrer un commentaire