Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename Iterator, typename Predicate = std::greater<>>
- void bubbleSort(Iterator begin, Iterator end, Predicate pred = Predicate())
- {
- if (begin == end) return;
- for (auto i = begin; i != end; ++i)
- {
- for (auto j = begin; std::next(j) != end; ++j)
- {
- if (pred(*j, *std::next(j)))
- {
- std::iter_swap(j, std::next(j));
- }
- }
- }
- }
- void sortTest()
- {
- std::vector<int> data = {5, 3, 8, 1, 2};
- // Sort in ascending order (default)
- bubbleSort(data.begin(), data.end());
- for (const auto &val : data)
- {
- std::cout << val << " ";
- }
- std::cout << std::endl;
- // Sort in descending order
- bubbleSort(data.begin(), data.end(), std::less<>());
- for (const auto &val : data)
- {
- std::cout << val << " ";
- }
- std::cout << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement