What is wrong with my Merge sort code? I am getting wrong output : I am implementing merge sort algorithm. I have debugged the code properly.
 but not able to identify what is the problem with my code.
Here is my code :

    #include<iostream>
    using namespace std;
    void merge(int* l,int nL,int* r,int nR,int * a){
        //merging the arrays
        int i=0,j=0,k=0;
        while(i<nL && j<nR){
            if(l[i]<=r[j]){
                i++;
                a[k]=l[i];
            }
    
            else{
                j++;
                a[k]=r[j];
            }
            k++;
        }
        //now elements that are left out
        while(i<nL){
            a[k]=l[i];
            k++;
            i++;
        }
        while(j<nR){
            a[k]=r[j];
            j++;
            k++;
        }
    }

This is my **mergeSort** function to implement the merge sort algorithm.

    void mergeSort(int* a,int n){
        //base case
        if(n<2)
            return;
        //rec case
        int mid=n/2;
        //take 2 arrays of size mid & (n-mid)
        int nL=mid;
        int nR=n-mid;
        int l[nL];
        int r[nR];
        //fill the arrays
        for(int i=0;i<mid;i++){
            l[i]=a[i];
        }
        for(int i=mid;i<n;i++){
            r[i-mid]=a[i];
        }
        //call merge sort recursively
        mergeSort(l,nL);
        mergeSort(r,nR);
        merge(l,nL,r,nR,a);
    }

Here is the main function where I am taking an array as input and passing to the function **mergeSort**

    int main(){
        int a[100];
        cout<<"Enter no of elements"<<endl;
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        mergeSort(a,n);
        cout<<"After sorting with merge sort"<<endl;
        for(int i=0;i<n;i++){
            cout<<a[i]<<" ";
        }
    
        return 0;
    }

Function **merge()** is merging two arrays and **mergeSort** is the function to divide the array and implement the merge sort.

I am giving this as input :

    8
    2 4 1 6 8 5 3 7

Output : 

    6 1006565088 2096014825 6 2098806136 2096014825 93 8 



