本文共 2567 字,大约阅读时间需要 8 分钟。
DataType | C(library) | C(hand-coded) | NumericalRecipes | Ratio1 | C++(C array) | C++(vector class) | Ratio2 |
---|---|---|---|---|---|---|---|
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 |
---|---|---|---|---|---|---|
![]() | ![]() | ![]() | Yes | Exchanging | ||
— | ![]() | ![]() | Yes | Exchanging | ||
— | — | ![]() | No | Exchanging | Small code size | |
— | ![]() | ![]() | Yes | Exchanging | Tiny code size | |
![]() | ![]() | ![]() | No | Selection | ||
![]() | ![]() | ![]() | Yes | Insertion | Average case is also ![]() | |
— | ![]() | ![]() | No | Insertion | ||
![]() | ![]() | ![]() | Yes | Insertion | When using a | |
![]() | ![]() | ![]() | Yes | Insertion | ||
![]() | ![]() | ![]() | Yes | Merging | ||
![]() | ![]() | ![]() | No | Merging | Example implementation here: ; can be implemented as a stable sort based on stable in-place merging: | |
![]() | ![]() | ![]() | No | Selection | ||
— | ![]() | ![]() | No | Selection | An - ![]() ![]() | |
![]() | ![]() | ![]() | No | Partitioning | variants use ![]() | |
![]() | ![]() | ![]() | No | Hybrid | Used in implementations | |
— | ![]() | ![]() | No | Insertion & Selection | Finds all the within O(n log n) | |
![]() | ![]() | ![]() | Yes | Selection | ||
![]() | ![]() | 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/