博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
排序算法@c++描述-插入排序
阅读量:4192 次
发布时间:2019-05-26

本文共 3364 字,大约阅读时间需要 11 分钟。

1.插入排序

普通版本

#include 
#include
#include
using namespace std;template
void insertionSort(vector
&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
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.outTotal time: 0.002ms1 20 56 190 435 834 923 934 8954

STL实现版本(模仿STL sort() 接口)

#include 
#include
#include
using namespace std;// 模仿STL sort()接口template
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
void insertionSort(const Iterator &begin, const Iterator &end, Functor func){ insertionSort(begin, end, func, *begin);}template
void _insertionSort(const Iterator &begin, const Iterator &end, const Object &obj){ insertionSort(begin, end, less
());}template
void insertionSort(const Iterator &begin, const Iterator &end){ _insertionSort(begin, end, *begin);}int main(){ vector
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.outTotal time: 0.005ms1 20 56 190 435 834 923 934 8954

仿函数

此时引入了仿函数(也叫函数结构,functor),可以对排序顺序进行自定义

比如可以利用lambda讲排序结果反过来:

#include 
#include
#include
using namespace std;// 模仿STL sort()接口template
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
void insertionSort(const Iterator &begin, const Iterator &end, Functor func){ insertionSort(begin, end, func, *begin);}template
void _insertionSort(const Iterator &begin, const Iterator &end, const Object &obj){ insertionSort(begin, end, less
());}template
void insertionSort(const Iterator &begin, const Iterator &end){ _insertionSort(begin, end, *begin);}int main(){ vector
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.outTotal time: 0.006ms8954 934 923 834 435 190 56 20 1

复杂度分析

最坏情况

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

时间复杂度:

O(n2)

最好情况

T(N)=N1

时间复杂度:

O(n)


  • 我的个人主页:
  • 我的个人站点博客:
  • 我的CSDN博客:
  • 我的简书:
  • 我的GitHub:

转载地址:http://enloi.baihongyu.com/

你可能感兴趣的文章
汽车之家港股上市发行价定为176.3港元 募资35.6亿港元
查看>>
中国首富又换人了!
查看>>
996问题已经失控,委员建议进行监管!网友拍手称快...
查看>>
基金大跌,基民上闲鱼“卖货回血”了!支付宝深夜发文!真的没人买基了?...
查看>>
《阿凡达》3月12日内地重映:部分影院已开启预售
查看>>
官方正式预热小米10S:哈曼卡顿加持小米有史以来音质最好的手机
查看>>
电影《你好,李焕英》进入全球票房榜前100
查看>>
不用再更换整机了,苹果官方可修复iPhone 12系列破裂后盖玻璃
查看>>
消息称网易云音乐寻求在港上市 或于明年正式IPO
查看>>
力压腾讯!《原神》连续5个月成中国手游海外收入冠军
查看>>
小镇青年“魂断”博彩暴富梦
查看>>
小米10S继承“祖传”三重快充:50W有线+30W无线+10W反充
查看>>
华为公开“实现汽车中电子控制功能的系统”相关专利
查看>>
华为Mate40 RS保时捷设计推8+256GB版本:起售价便宜1000元
查看>>
美团股价盘中涨幅超7% 市值重回2万亿港元关口
查看>>
微信又更新了!支持上班摸鱼了
查看>>
《山河令》火爆,人人都想靠耽改剧赌一把
查看>>
让AI打工!搜狗全体员工于3月12日狗胜节放假一天
查看>>
干得漂亮!以后这些内容朋友圈都不能发了
查看>>
还在4S店买车?《Boss1+1》张朝阳对话贾鸣镝“种草”购车新方式
查看>>