Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. class FunctorIncrease
  5. {
  6. public:
  7.     template <typename T>
  8.     bool operator()(T a, T b)
  9.     {
  10.         if(a>b)
  11.             return true;
  12.         else
  13.             return false;
  14.     }
  15. };
  16.  
  17. template <typename T, typename F = FunctorIncrease>
  18. void Bubble(T *begin, T *end, F f)
  19. {
  20.     for(T *i = begin; i!=end-1;i++)
  21.     {
  22.         for(T *j = begin; j!= end-1; j++)
  23.         {
  24.             if(f(*j,*(j+1))==true)
  25.             {
  26.                 T temp = *j;
  27.                 *j = *(j+1);
  28.                 *(j+1) = temp;
  29.             }      
  30.         }
  31.     }
  32. }
  33.  
  34. int main()
  35. {
  36.     int a[10];
  37.     FunctorIncrease f1;
  38.     srand(time(0));
  39.     for(int i = 0;i < 10;i++)
  40.     {
  41.         a[i] = rand()%10+1;;
  42.     }
  43.  
  44.     Bubble(a,a+10);
  45.  
  46.     for(int i = 0;i < 10;i++)
  47.     {
  48.         std::cout << a[i] << " ";
  49.     }
  50.  
  51.     std:: cout << std::endl;
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement