排序算法@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 

复杂度分析

最坏情况

T(N)=1+2+3+...+(N1)

时间复杂度:

O(n2)

最好情况

T(N)=N1

时间复杂度:

O(n)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值