Advertisement
bartekltg

prosty qsort openmp

Jul 22nd, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. template <class T,class comp>
  2. T mediana (const T &a,const T &b,const T &c, comp cmp)
  3. {
  4.     if (cmp(a,b)) //a<b
  5.     {
  6.         if (cmp(b,c)) // a<b<c
  7.             return b;
  8.         else //b=max
  9.             return (cmp(a,c))?c:a;
  10.     } else//b<a
  11.     {
  12.         if (cmp(a,c)) // b<a<c
  13.             return a;
  14.         else //a=max
  15.             return (cmp(b,c))?c:b;
  16.     }
  17.  
  18. }
  19.  
  20. template <class iterator,class comp>
  21. void par_sort(iterator begin, iterator end, comp cmp)
  22. {
  23.  
  24.     int a= end-begin;
  25.  
  26.     if (end-begin<1000)
  27.     {
  28.         sort(begin, end, cmp);
  29.     }
  30.     else
  31.     {
  32.  
  33.         iterator::value_type el_dzielacy =  mediana(*(begin + ((end-begin)/2)),*begin,*(end-1),cmp);
  34.         iterator podzial = partition(begin, end, [=](const iterator::value_type &a ){return cmp(a,el_dzielacy);}  ) ;
  35.  
  36.         int aa=end-podzial;
  37.         int bb= podzial-begin;
  38.         if (aa==0 || bb==0)
  39.         {
  40.             sort(begin,end);
  41.             return;
  42.         }
  43.  
  44.         #pragma omp parallel sections
  45.         {
  46.             #pragma omp section
  47.                  par_sort( podzial,  end,  cmp);
  48.             #pragma omp section
  49.                  par_sort( begin, podzial,  cmp);
  50.         }
  51.  
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement