Sort and Search in C and C++

Sorting in C++ VS. C

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
int5.90-5.92 1.54-1.65 1.46-1.50 3.7 1.12-1.16 1.11-1.14 5.3
short9.03-9.03 1.73-1.80 1.58-1.59*5.1 1.17-1.20 1.17-1.19 7.7
byte7.87-7.89 0.98-1.02 0.98-1.00 7.9 0.70-0.73 0.70-0.73 11.0
float7.08-7.10 2.38-2.50 2.48-2.55 2.9 1.96-2.02 1.97-2.02 3.6
double16.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.

 

 

C++ 排序函数 sort(),qsort()的用法

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 /mathcal{O} /left( n^2 /right)  /mathcal{O} /left( n^2 /right)  /mathcal{O} /left( {1} /right)YesExchanging 
Cocktail sort /mathcal{O} /left( n^2 /right) /mathcal{O}/left( {1} /right)YesExchanging 
Comb sort/mathcal{O}/left( {1} /right)NoExchangingSmall code size
Gnome sort /mathcal{O} /left( n^2 /right) /mathcal{O}/left( {1} /right)YesExchangingTiny code size
Selection sort /mathcal{O} /left( n^2 /right)  /mathcal{O} /left( n^2 /right) /mathcal{O}/left( {1} /right)NoSelection 
Insertion sort /mathcal{O} /left( n^2 /right)  /mathcal{O} /left( n^2 /right) /mathcal{O}/left( {1} /right)YesInsertionAverage case is also /mathcal{O}/left( {n + d} /right), where d is the number of inversions
Shell sort/mathcal{O}/left( {n /log^2 n} /right)/mathcal{O}/left( {1} /right)NoInsertion 
Binary tree sort/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( n /right)YesInsertionWhen using a self-balancing binary search tree
Library sort/mathcal{O}/left( {n /log n} /right) /mathcal{O} /left( n^2 /right) /mathcal{O}/left( n /right)YesInsertion 
Merge sort/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( n /right)YesMerging 
In-place merge sort/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( {1} /right)NoMergingExample implementation here: [1]; can be implemented as a stable sort based on stable in-place merging: [2]
Heapsort/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( {1} /right)NoSelection 
Smoothsort/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( {1} /right)NoSelectionAn adaptive sort - /mathcal{O}/left( {n} /right) comparisons when the data is already sorted, and /mathcal{O}/left( {0} /right) swaps.
Quicksort/mathcal{O}/left( {n /log n} /right) /mathcal{O}/left( n^2 /right)/mathcal{O}/left( {/log n} /right)NoPartitioningNaïve variants use  /mathcal{O} /left( n /right) space
Introsort/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( {n /log n} /right)/mathcal{O}/left( {/log n} /right)NoHybridUsed in SGI STL implementations
Patience sorting /mathcal{O} /left( {n /log n} /right) /mathcal{O}/left( n /right)NoInsertion & SelectionFinds all the longest increasing subsequences within O(n log n)
Strand sort/mathcal{O}/left( {n /log n} /right) /mathcal{O} /left( {n^2} /right) /mathcal{O}/left( n /right)YesSelection 
Tournament sort/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次交换

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值