Merge Without Extra Space - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Thursday 11 July 2019

Merge Without Extra Space


 Expected time complexity is O((n+m) log(n+m)). DO NOT use extra space.  We need to modify existing arrays as following.

The idea is to we start comparing elements that are far from each other rather than adjacent.
For every pass, we calculate the gap and compare the elements towards the right of the gap. Every pass, the gap reduces to the ceiling value of dividing by 2.
Method 1:
#include <iostream>
using namespace std;
int nextGap(int gap) 

    if (gap <= 1) 
        return 0; 
    return (gap / 2) + (gap % 2); 

  
void merge(long long *arr1, long long *arr2, int n, int m) 

    int i, j, gap = n + m; 
    for (gap = nextGap(gap); gap > 0; gap = nextGap(gap)) 
    { 
        // comparing elements in the first array. 
        for (i = 0; i + gap < n; i++) 
            if (arr1[i] > arr1[i + gap]) 
                swap(arr1[i], arr1[i + gap]); 
  
        //comparing elements in both arrays. 
        for (j = gap > n ? gap-n : 0 ; i < n&&j < m; i++, j++) 
            if (arr1[i] > arr2[j]) 
                swap(arr1[i], arr2[j]); 
  
        if (j < m) 
        { 
            //comparing elements in the second array. 
            for (j = 0; j + gap < m; j++) 
                if (arr2[j] > arr2[j + gap]) 
                    swap(arr2[j], arr2[j + gap]); 
        } 
    } 

int main() {
//code
int t;
cin>>t;
while(t--){
    int n,m,i,j;
    cin>>n>>m;
    long long a[n],b[m];
    for(i=0;i<n;i++){
        cin>>a[i];
    }
    for(i=0;i<m;i++){
        cin>>b[i];
    }
    merge(a,b,n,m);
    for(i=0;i<n;i++){
        cout<<a[i]<<" ";
    }
    for(i=0;i<m;i++){
        cout<<b[i]<<" ";
    }
    cout<<endl;
}
return 0;

}