SHARE
TWEET

Mergesort(Tanima.begin(), Tanima.end())

a guest May 22nd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[int(1e5+5)];
  4. vector<int> left, right;
  5.  
  6. void mergesort(int n, int l, int r)
  7. {
  8.     if (l>=r) return;
  9.     int mid=(l+r)/2;
  10.     mergesort(n, l, mid);
  11.     mergesort(n, mid, r);
  12.     merge(l,r,mid);
  13. }
  14.  
  15. void merge(int l, int r, int mid)
  16. {
  17.     for (int i=l; i<mid; i++) {
  18.         left.push_back(a[i]);
  19.     }
  20.     for (int i=mid; i<r; i++) {
  21.         right.push_back(a[i]);
  22.     }
  23.     int i=0, j=0, k=l;
  24.     while (i<(mid-l+1) and j<(r-mid+1)) {
  25.         if (left[i]<=right[j])
  26.             a[k++]=left[i++];
  27.         else a[k++]=right[j++];
  28.         while (i<mid-l+1)
  29.             a[k++]=left[i++];
  30.         while (j<r-mid+1)
  31.             a[k++]=right[j++];
  32.     }
  33. }
  34.  
  35. int main()
  36. {
  37.     int n;
  38.     cin>>n;
  39.     for (int i=0; i<n; i++)
  40.         cin>>a[i];
  41.     mergesort(n, 0, n-1);
  42.     for (int i=0; i<n; i++)
  43.         cout<<a[i]<<" ";
  44.     cout<<endl;
  45.     return 0;
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top