冒泡排序——C++实现
冒泡排序——C++实现// bubble_sort.cpp#include <iostream>#include "../head_file/template_head.h"#include <vector>using namespace std;void bubble_sort(){cout << "Bubble Sort!!!" << endl;
·
冒泡排序——C++实现
通用头文件:template_head.h
// bubble_sort.cpp
#include <iostream>
#include "../head_file/template_head.h"
#include <vector>
using namespace std;
void bubble_sort()
{
cout << "Bubble Sort!!!" << endl;
vector<int> list = { 8,9,1,7,2,3,5,4,6,0,11,9,8 };
bubbleSort(list.begin(), list.end(), greater_<int>{});
printList(list);
}
template <typename Iterator>
void bubbleSort(const Iterator& begin, const Iterator& end) {
bubbleSort(begin, end, less_<decltype(*begin)>{});
}
template <typename Iterator, typename Comparator>
void bubbleSort(const Iterator& begin, const Iterator& end, Comparator lessThan) {
auto size = end - begin;
for (Iterator i = begin; i != end; i++) {
for (Iterator j = begin; j != end - 1 - (i - begin); j++) {
if (!lessThan(*j, *(j + 1))) {
std::swap(*j, *(j + 1));
}
}
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)