Merge Sort | Practice | GeeksforGeeks

Given an array arr[], its starting position l and its ending position r. Sort the array using merge sort algorithm.
Example 1:
Input:
N = 5
arr[] = {4 1 3 9 7}
Output:
1 3 4 7 9

Example 2:
Input:
N = 10
arr[] = {10 9 8 7 6 5 4 3 2 1}
Output:
1 2 3 4 5 6 7 8 9 10

Constraints:
1 <= N <= 105
1 <= arr[i] <= 103
0
adityakmcs2 days ago
this code giving me runtime error pls help
  
 
 
 void merge(int arr[], int l, int m, int h)   {        // Your code here        int i=l,j=m+1,k=l;        int B[h+1];                while(i<=m&&j<=h){                        if(arr[i]<arr[j])            B[k++]=arr[i++];            else            B[k++]=arr[j++];                    }        for(;i<=j;i++){            B[k++]=arr[j++];        }        for(;j<=h;j++){            B[k++]=arr[j];        }        for(int i=l;i<=j;i++){            arr[i]=B[i];        }       //  return;           }   public:   void mergeSort(int arr[], int l, int h)   {             //code here       if(l<h){            int mid = (l+h)/2;            mergeSort(arr,l,mid);            mergeSort(arr,mid+1,h);            merge(arr,l,mid,h);       }              // return ;          }
0
shanu145 days ago
void merge(int arr[], int l, int m, int r)
    {
         // Your code here
         int len1=m-l+1;
         int len2=r-m;
         int* a=new int[len1];
         int* b=new int[len2];
         
         int k=l;
         for(int i=0;i<len1;i++){
             a[i]=arr[k++];
         }
         k=m+1;
         for(int i=0;i<len2;i++){
             b[i]=arr[k++];
         }
         int index1=0;
         int index2=0;
         k=l;
         while(index1<len1&&index2<len2){
             if(a[index1]<b[index2]){
                 arr[k++]=a[index1++];
             }
             else{
                 arr[k++]=b[index2++];
             }
         }
         while(index1<len1){
             arr[k++]=a[index1++];
         }
         while(index2<len2){
             arr[k++]=b[index2++];
         }
    }
    public:
    void mergeSort(int arr[], int l, int r)
    {
        //code here
        int mid=(l+r)/2;
        if(l<r){
            mergeSort(arr,l,mid);
            mergeSort(arr,mid+1,r);
            merge(arr,l,mid,r);
        }
    }
-1
tejaswayadav2 weeks ago
Java Solution, but gives TLE:class Solution{   void merge(int arr[], int l, int m, int r)   {              int[] newArr = new int[r-l+1];              int i = l, j=m +1, k=0;              while(i <= m && j <= r){           if(arr[i] <= arr[j]) {               newArr[k++] = arr[i++];           } else{               newArr[k++] = arr[j++];           }       }              while(i <= m) {           newArr[k++] = arr[i++];       }              while(j <= r) {           newArr[k++] = arr[j++];       }              for (i=l; i<=r; i++) {           arr[i] = newArr[i-l];       }          }   void mergeSort(int arr[], int l, int r) {              if(l<r) {           int m = (l+r)/2;           mergeSort(arr,l,m);           mergeSort(arr,m+1,r);           merge(arr, l, m, r);       }   }}
0
karunakarmutthi3 weeks ago
can anyone say why code is showing TLE in 355 test case
 
void merge(int arr[], int l, int mid, int r)   {      int[] b=new int[arr.length];       int i=l;       int j=mid+1;       int k=l;       while(i<=mid&&j<=r){           if(arr[i]<=arr[j]){               b[k++]=arr[i++];           }else{               b[k++]=arr[j++];           }       }       if(i>mid){           while(j<=r){               b[k++]=arr[j++];           }       }else{          while(i<=mid){              b[k++]=arr[i++];          }        }       for(int p=l;p<=r;p++){           arr[p]=b[p];       }    }   void mergeSort(int arr[], int l, int r)   {       if(l<r){           int mid=(l+r)/2;           mergeSort(arr,l,mid);           mergeSort(arr,mid+1,r);           merge(arr,l,mid,r);       }   }
0
alokkumar90193 weeks ago
void merge(int arr[], int l, int m, int r)
   {
        // Your code here
        int n1=m-l+1, n2=r-m;
        int left[n1], right[n2];
        for(int i=0; i<n1; i++){
            left[i]=arr[l+i];
        }
        for(int i=0; i<n2; i++){
            right[i]=arr[m+1+i];
        }
        int i=0, j=0, k=l;
        while(i<n1 && j<n2){
            if(left[i]<=right[j]){
                arr[k++]=left[i++];
            } else{
                arr[k++]=right[j++];
            }
        }
        while(i<n1){
            arr[k++]=left[i++];
        }
        while(j<n2){
            arr[k++]=right[j++];
        }
   }
   public:
   void mergeSort(int arr[], int l, int r)
   {
       //code here
       if(r>l){
            int m=l+(r-l)/2;
            mergeSort(arr,l,m);
            mergeSort(arr,m+1,r);
            merge(arr,l,m,r);
        }
   }
0
alokkumar9019
This comment was deleted.
0
namanv083 weeks ago
100% working c++ code
 
#include<iostream>
using namespace std;

void merge(int arr[],int l,int mid,int h){
	if(l>=h){
		return;
	}
    int len1=mid-l+1;
    int len2=h-mid;
    int first[len1];
    int second[len2];
    for(int i=0;i<len1;i++){
        first[i]=arr[l+i];
    }
    for(int j=0;j<len2;j++){
        second[j]=arr[mid+1+j];
    }
    int mainIndex=l;
    int index1=0,index2=0;
    while( index1<len1 && index2<len2){
        if(first[index1]<=second[index2]){
            arr[mainIndex]=first[index1];
            mainIndex++;
            index1++;
        }
        else{
            arr[mainIndex]=second[index2];
            mainIndex++;
            index2++;
        }
    }
    while(index1<len1){
        arr[mainIndex++]=first[index1++];
    }
    while(index2<len2){
        arr[mainIndex++]=second[index2++];
    }
//    delete[] first;
//    delete[] second;
}
void mergesort(int arr[],int l,int h){
    if(l>=h){
    	return ;
	}
    int mid=l+(h-l)/2;
    mergesort(arr,l,mid);
    mergesort(arr,mid+1,h);
    merge(arr,l,mid,h);
}
int main(){
    int n;
    cout<<"Enter array size"<<" ";
    cin>>n;
    int arr[n];
    cout<<"Enter array elements"<<" ";
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    mergesort(arr,0,n-1);
    for(int i=0;i<n;i++){
    	cout<<arr[i]<<" ";
	}
    return 0;
}
0
dhamanedivya9991 month ago
what is wrong with this code?
 
void merge(int arr[], int l, int m, int r)   {       int i =l;       int j = m+1;       int k =l;       int b[500];              while(i<=m && j<=r){           if(arr[i]<arr[j]){              b[k++]= arr[i++];           }           else{               b[k++]=arr[j++];           }       }                     if(i>m){        while(j<=r){           b[k++]=arr[j++];        }       }       else{        while(i<=m){           b[k++]=arr[i++];        }       }                     for( k =l;k<=r;k++){           arr[k] =  b[k];       }   }         public:   void mergeSort(int arr[], int l, int r)   {       if(l>=r){           return;       }                  int m = l+(r-l)/2;           mergeSort(arr,l,m);           mergeSort(arr,m+1,r);           merge(arr,l,m,r);       }
0
dawsonquadros
This comment was deleted.
0
amishasahu3281 month ago
void merge(int arr[], int l, int m, int r)
    {
         // Your code here
         int n1 = m-l+1, n2 = r-m;
         int left[n1], right[n2];
         for(int i = 0; i < n1; i++)
            left[i] = arr[l+i];
         for(int i = 0; i < n2; i++)
            right[i] = arr[m+i+1];
        
        int i = 0, j = 0, k = l;
        while(i < n1 && j < n2)
        {
            if(left[i] <= right[j])
                arr[k++] = left[i++];
            else
                arr[k++] = right[j++];
        }
        while(i < n1)
        {
            arr[k++] = left[i++];
        }
        while(j < n2)
        {
            arr[k++] = right[j++];
        }
    }
    public:
    void mergeSort(int arr[], int l, int r)
    {
        //code here
        if(l >= r)
            return;
            
            int m = (l+r)/2;
            mergeSort(arr, l, m);
            mergeSort(arr, m+1, r);
            merge(arr, l, m, r);
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.