排序算法@c++描述-插入排序

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 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,287评论 25 708
  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 11,218评论 12 197
  • 感恩今天是非常顺利的一天,感觉天气越来越好 感恩一切美好的事情都在我的身上发生 感恩帮灵姨买了护肝片两瓶 感恩帮助...
    生活就该甜甜蜜蜜阅读 146评论 0 1
  • 看到这样一幅画,觉得很有意思。或许有一天,我们真能穿越回到春秋战国…
    黑桃皇阅读 285评论 3 4
  • 昨晚,一已退休多年的兄长请我去赤坎喝酒。 本来,他还请另外几位朋友来喝的,但有的不接电话,有的推托不肯来。好在我请...
    弟哥阅读 365评论 0 0