http://theory.stanford.edu/~amitp/rants/c++-vs-c/
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.
http://blog.csdn.net/zzzmmmkkk/archive/2009/06/13/4266888.aspx
函数名 | 功能描述 |
---|---|
sort | 对给定区间所有元素进行排序 |
stable_sort | 对给定区间所有元素进行稳定排序 |
partial_sort | 对给定区间所有元素部分排序 |
partial_sort_copy | 对给定区间复制并排序 |
nth_element | 找出给定区间的某个位置对应的元素 |
is_sorted | 判断一个区间是否已经排好序 |
partition | 使得符合某个条件的元素放在前面 |
stable_partition | 相对稳定的使得符合某个条件的元素放在前面 |
Comparison of algorithms
http://en.wikipedia.org/wiki/Sorting_algorithm
Name ![]() | Average ![]() | Worst ![]() | Memory ![]() | Stable ![]() | Method ![]() | Other notes ![]() |
---|---|---|---|---|---|---|
Bubble sort | ![]() | ![]() | ![]() | Yes | Exchanging | |
Cocktail sort | — | ![]() | ![]() | Yes | Exchanging | |
Comb sort | — | — | ![]() | No | Exchanging | Small code size |
Gnome sort | — | ![]() | ![]() | Yes | Exchanging | Tiny code size |
Selection sort | ![]() | ![]() | ![]() | No | Selection | |
Insertion sort | ![]() | ![]() | ![]() | Yes | Insertion | Average case is also ![]() |
Shell sort | — | ![]() | ![]() | No | Insertion | |
Binary tree sort | ![]() | ![]() | ![]() | Yes | Insertion | When using a self-balancing binary search tree |
Library sort | ![]() | ![]() | ![]() | Yes | Insertion | |
Merge sort | ![]() | ![]() | ![]() | Yes | Merging | |
In-place merge sort | ![]() | ![]() | ![]() | No | Merging | Example implementation here: [1]; can be implemented as a stable sort based on stable in-place merging: [2] |
Heapsort | ![]() | ![]() | ![]() | No | Selection | |
Smoothsort | — | ![]() | ![]() | No | Selection | An adaptive sort - ![]() ![]() |
Quicksort | ![]() | ![]() | ![]() | No | Partitioning | Naïve variants use ![]() |
Introsort | ![]() | ![]() | ![]() | No | Hybrid | Used in SGI STL implementations |
Patience sorting | — | ![]() | ![]() | No | Insertion & Selection | Finds all the longest increasing subsequences within O(n log n) |
Strand sort | ![]() | ![]() | ![]() | Yes | Selection | |
Tournament sort | ![]() | ![]() | Selection |
冒泡排序: 稳定, O(n2)。
选择排序: 不稳定, O(n2)。
堆排序: 不稳定, O(n log n)。
快速排序: 不稳定, O(n log n)。
希尔排序:O(n1.25)。
基数排序:O(n)。
桶排序:。
选择排序的平均时间复杂度比冒泡排序的稍低:
同样数据的情况下,2种算法的循环次数是一样的,但选择排序只有0到1次交换,而冒泡排序只有0到n次交换
希尔排序实质上是一种分组插入方法。