1.插入排序
普通版本
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
template <typename T>
void insertionSort(vector<T> &a)
{
int i;
for (int j = 1; j < a.size(); j++) {
T tmp = a[j];
for (i = j; i > 0 && tmp < a [i - 1]; i--)
a[i] = a[i - 1];
a[i] = tmp;
}
}
int main()
{
vector<int> test = {190, 435, 834, 8954, 923, 56, 20 ,1, 934};
clock_t time_stt = clock();
insertionSort(test);
cout << "Total time: " << 1000 * (clock() - time_stt) / (double)CLOCKS_PER_SEC << "ms" << endl;
for (auto i : test)
cout << i << " ";
cout << endl;
return 0;
}
- 运行结果:
$ ./a.out
Total time: 0.002ms
1 20 56 190 435 834 923 934 8954
STL实现版本(模仿STL sort()
接口)
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
// 模仿STL sort()接口
template <typename Iterator, typename Object, typename Functor>
void insertionSort(const Iterator &begin, const Iterator &end, Functor func, const Object &obj)
{
Iterator i;
for (Iterator j = begin + 1; j != end; j++) {
Object tmp = *j;
for (i = j; i != begin && func(tmp, *(i - 1)); i--)
*i = *(i - 1);
*i = tmp;
}
}
template <typename Iterator, typename Functor>
void insertionSort(const Iterator &begin, const Iterator &end, Functor func)
{
insertionSort(begin, end, func, *begin);
}
template <typename Iterator, typename Object>
void _insertionSort(const Iterator &begin, const Iterator &end, const Object &obj)
{
insertionSort(begin, end, less<Object>());
}
template <typename Iterator>
void insertionSort(const Iterator &begin, const Iterator &end)
{
_insertionSort(begin, end, *begin);
}
int main()
{
vector<int> test = {190, 435, 834, 8954, 923, 56, 20 ,1, 934};
clock_t time_stt = clock();
insertionSort(test.begin(), test.end());
cout << "Total time: " << 1000 * (clock() - time_stt) / (double)CLOCKS_PER_SEC << "ms" << endl;
for (auto i : test)
cout << i << " ";
cout << endl;
return 0;
}
- 运行结果:
$ ./a.out
Total time: 0.005ms
1 20 56 190 435 834 923 934 8954
仿函数
此时引入了仿函数(也叫函数结构,functor),可以对排序顺序进行自定义
比如可以利用lambda讲排序结果反过来:
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
// 模仿STL sort()接口
template <typename Iterator, typename Object, typename Functor>
void insertionSort(const Iterator &begin, const Iterator &end, Functor func, const Object &obj)
{
Iterator i;
for (Iterator j = begin + 1; j != end; j++) {
Object tmp = *j;
for (i = j; i != begin && func(tmp, *(i - 1)); i--)
*i = *(i - 1);
*i = tmp;
}
}
template <typename Iterator, typename Functor>
void insertionSort(const Iterator &begin, const Iterator &end, Functor func)
{
insertionSort(begin, end, func, *begin);
}
template <typename Iterator, typename Object>
void _insertionSort(const Iterator &begin, const Iterator &end, const Object &obj)
{
insertionSort(begin, end, less<Object>());
}
template <typename Iterator>
void insertionSort(const Iterator &begin, const Iterator &end)
{
_insertionSort(begin, end, *begin);
}
int main()
{
vector<int> test = {190, 435, 834, 8954, 923, 56, 20 ,1, 934};
clock_t time_stt = clock();
insertionSort(test.begin(), test.end(), [](int a, int b){
if (a > b)
return 1;
else
return 0;
});
cout << "Total time: " << 1000 * (clock() - time_stt) / (double)CLOCKS_PER_SEC << "ms" << endl;
for (auto i : test)
cout << i << " ";
cout << endl;
return 0;
}
- 运行结果:
$ ./a.out
Total time: 0.006ms
8954 934 923 834 435 190 56 20 1
- 我的个人主页:http://www.techping.cn/
- 我的个人站点博客:http://blog.techping.cn/
- 我的CSDN博客:http://blog.csdn.net/techping
- 我的简书://www.greatytc.com/users/b2a36e431d5e/timeline
- 我的GitHub:https://github.com/techping