博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sort and Search in C and C++
阅读量:2360 次
发布时间:2019-05-10

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

Data
Type
C
(library)
C
(hand-coded)
Numerical
Recipes
Ratio
1
C++
(C array)
C++
(vector class)
Ratio
2
int 5.90-5.92 1.54-1.65 1.46-1.50 3.7 1.12-1.16 1.11-1.14 5.3
short 9.03-9.03 1.73-1.80 1.58-1.59* 5.1 1.17-1.20 1.17-1.19 7.7
byte 7.87-7.89 0.98-1.02 0.98-1.00 7.9 0.70-0.73 0.70-0.73 11.0
float 7.08-7.10 2.38-2.50 2.48-2.55 2.9 1.96-2.02 1.97-2.02 3.6
double 16.42-16.45 2.70-2.93 2.72-2.83 5.8 2.28-2.35 2.28-2.37 7.1

 

Conclusions

I can’t claim that C++ is always faster than C. However, I do claim that C++ templates offer a way to provide library routines that offer the traditional advantages of library routines (ease of use, flexibility) yet still are able to provide speed close to hand-written code. This combination is generally unavailable to programmers using C (or Pascal, or Basic, or Java, or …). When I programmed in C, I always had to choose between library routines and hand-written code. In C++, I choose (STL) library routines whenever possible. I usually get better (faster and more flexible) code than I would have written myself (without putting in a lot of effort). It’s refreshing to finally be able to use library routines without expecting any performance penalties.

 

 

sort 对给定区间所有元素进行排序
stable_sort 对给定区间所有元素进行稳定排序
partial_sort 对给定区间所有元素部分排序
partial_sort_copy 对给定区间复制并排序
nth_element 找出给定区间的某个位置对应的元素
is_sorted 判断一个区间是否已经排好序
partition 使得符合某个条件的元素放在前面
stable_partition 相对稳定的使得符合某个条件的元素放在前面

 

 

Comparison of algorithms

 

Name   Average   Worst   Memory   Stable   Method   Other notes  
 /mathcal{O} /left( n^2 /right)  /mathcal{O} /left( n^2 /right)  /mathcal{O} /left( {1} /right) Yes Exchanging  
 /mathcal{O} /left( n^2 /right) /mathcal{O}/left( {1} /right) Yes Exchanging  
/mathcal{O}/left( {1} /right) No Exchanging Small code size
 /mathcal{O} /left( n^2 /right) /mathcal{O}/left( {1} /right) Yes Exchanging Tiny code size
 /mathcal{O} /left( n^2 /right)  /mathcal{O} /left( n^2 /right) /mathcal{O}/left( {1} /right) No Selection  
 /mathcal{O} /left( n^2 /right)  /mathcal{O} /left( n^2 /right) /mathcal{O}/left( {1} /right) Yes Insertion Average case is also /mathcal{O}/left( {n + d} /right), where d is the number of
/mathcal{O}/left( {n /log^2 n} /right) /mathcal{O}/left( {1} /right) No Insertion  
/mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( n /right) Yes Insertion When using a
/mathcal{O}/left( {n /log n} /right)  /mathcal{O} /left( n^2 /right) /mathcal{O}/left( n /right) Yes Insertion  
/mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( n /right) Yes Merging  
/mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {1} /right) No Merging Example implementation here: ; can be implemented as a stable sort based on stable in-place merging:
/mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {1} /right) No Selection  
/mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {1} /right) No Selection An - /mathcal{O}/left( {n} /right) comparisons when the data is already sorted, and /mathcal{O}/left( {0} /right) swaps.
/mathcal{O}/left( {n /log n} /right)  /mathcal{O}/left( n^2 /right) /mathcal{O}/left( {/log n} /right) No Partitioning variants use  /mathcal{O} /left( n /right) space
/mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {/log n} /right) No Hybrid Used in implementations
 /mathcal{O} /left( {n /log n} /right) /mathcal{O}/left( n /right) No Insertion & Selection Finds all the within O(n log n)
/mathcal{O}/left( {n /log n} /right)  /mathcal{O} /left( {n^2} /right) /mathcal{O}/left( n /right) Yes Selection  
/mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( {n /log n} /right)     Selection
 

插入排序: 稳定, O(n2)。

冒泡排序: 稳定, O(n2)。

选择排序: 不稳定, O(n2)。

堆排序: 不稳定, O(n log n)。

快速排序: 不稳定, O(n log n)。

希尔排序:O(n1.25)。

基数排序:O(n)。

桶排序:。

 

选择排序的平均时间复杂度比冒泡排序的稍低: 

同样数据的情况下,2种算法的循环次数是一样的,但选择排序只有0到1次交换,而冒泡排序只有0到n次交换

希尔排序实质上是一种分组插入方法

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

你可能感兴趣的文章
数据湖正在成为新的数据仓库
查看>>
1个月获客100万,这个支付宝小程序如何打造生态运营的增长飞轮
查看>>
工程师如何给女友买包?问问阿里“百事通”
查看>>
运维编排场景系列----给实例加到SLS机器组
查看>>
阿里云发布边缘容器,云边端一体化时代来临
查看>>
分享 KubeCon 2019 (上海)关于 Serverless 及 Knative 相关演讲会议
查看>>
直击 KubeCon 2019 现场,阿里云 Hands-on Workshop 亮点回顾
查看>>
UI2CODE复杂背景无法识别?闲鱼工程师这样打造高准确率方案
查看>>
MaxCompute 费用暴涨之新增SQL分区裁剪失败
查看>>
千亿级的数据难题,优酷工程师怎么解决?
查看>>
阿里云环境中TLS/SSL握手失败的场景分析
查看>>
玩转运维编排服务的权限:Assume Role+Pass Role
查看>>
机器学习在高德搜索建议中的应用优化实践
查看>>
阿里开源新一代 AI 算法模型,由达摩院90后科学家研发
查看>>
实践:轻松可视化实现设备监控大屏效果
查看>>
阿里首部技术经验精选集:《不止代码》公开免费下载
查看>>
如何在云上使用confd+ACM管理敏感数据
查看>>
阿里资深技术专家的10年感悟
查看>>
AnalyticDB for MySQL 3.0基础版重磅发布
查看>>
MongoDB sharding 集合不分片性能更高?
查看>>