SHARE
TWEET

Merge Sort

keverman Sep 26th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void merger(std::vector<int> &T, int l, int m, int r)
  2. {
  3.     std::vector<int> LT, RT;
  4.  
  5.     for(int i = l; i <= m; i++) LT.push_back(T[i]);
  6.     for(int i = m + 1; i <= r; i++) RT.push_back(T[i]);
  7.  
  8.     int lit = 0, rit = 0;
  9.  
  10.     while(lit < LT.size() && rit < RT.size())
  11.         T[lit + rit + l] = (LT[lit] <= RT[rit] ? LT[lit++]: RT[rit++]);
  12.  
  13.     while(lit < LT.size()) T[lit + rit + l] = LT[lit++];
  14.     while(rit < RT.size()) T[lit + rit + l] = RT[rit++];
  15. }
  16.  
  17. void mergeSort(std::vector<int> &T, int l, int r)
  18. {
  19.     if(l < r)
  20.     {
  21.         int m = l + (r - l) / 2;
  22.  
  23.         mergeSort(T, l, m);
  24.         mergeSort(T, m + 1, r);
  25.         merger(T, l, m, r);
  26.     }
  27. }
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