sonprao

MERGESORT

Sep 16th, 2019
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Mergesort(int *a, int l, int r ,int *b, bool (*cmp)(int,int))
  2. {
  3.     int i,j,k,m;
  4.     if (r-l>0)
  5.     {
  6.         m=(r+l)/2;
  7.         Mergesort(a,l,m,b,cmp);
  8.         Mergesort(a,m+1,r,b,cmp);
  9.         for(i=m; i>=l;i--) *(b+i)=*(a+i);
  10.         FOR(j,m+1,r) *(b+r+m+1-j)=*(a+j);
  11.         //cout<<endl;
  12.         //FOR(k,1,n) cout<<b[k]<<" ";
  13.         //cout<<endl;
  14.         //cout<<i<<" "<<j<<" "<<l<<" "<<r<<endl;
  15.         //cout<<endl;
  16.         i++;
  17.         j--;
  18.         FOR(k,l,r)
  19.          if (cmp(*(b+i),*(b+j)))
  20.             {*(a+k)=*(b+i); i++;
  21.            // cout<<k<<" "<<a[k]<<endl;
  22.            }
  23.             else
  24.             {*(a+k)=*(b+j); j--;
  25.            // cout<<k<<" "<<a[k]<<endl;
  26.             }
  27.     }
  28.     return;
  29. }
RAW Paste Data