fork download
  1. #include <list>
  2. #include <functional>
  3. #include <iterator>
  4. #include <iostream>
  5. #include <random>
  6.  
  7. template <typename TIterator, typename TObject>
  8. void mergeSort(const TIterator& begin, const TIterator& end,
  9. std::function<bool (const TObject& left,
  10. const TObject& right)> criterium)
  11. {
  12. //...
  13. }
  14.  
  15. bool compare(int a, int b)
  16. {
  17. return a < b;
  18. }
  19.  
  20. int main(int argc, char** argv) // And now to test the algorithm
  21. {
  22. std::list<int> container;
  23. for (int i = 0; i < 100; ++i)
  24. container.push_back(random() % 20);
  25.  
  26. std::function<bool(const int&, const int&)> myCompare = compare;
  27. mergeSort(container.begin(), container.end(), myCompare);
  28.  
  29. for (auto it = container.begin(); it != container.end(); ++it)
  30. std::cout << (*it) << std::endl;
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
3
6
17
15
13
15
6
12
9
1
2
7
10
19
3
6
0
6
12
16
11
8
7
9
2
10
2
3
7
15
9
2
2
18
9
7
13
16
11
2
9
13
1
19
4
17
18
4
15
10
13
6
11
0
16
13
2
10
16
1
5
5
4
7
16
5
6
9
13
17
4
15
2
5
14
7
14
4
3
10
7
8
16
18
8
4
3
11
14
19
12
0
16
8
19
12
6
6
14
19