Comparison of different sorting algorithms
Bubble sort 冒泡排序
Specifically, we can improve the crude version of bubble sort given above by exploiting the following observation: if a pass through the list makes no exchanges, the list has been sorted and we can stop the algorithm.
Though the new version runs faster on some inputs, it is still in Θ ( n 2 ) \Theta(n^2) Θ(n2) in the worst and average cases.
Selection sort 选择排序
Θ ( n 2 ) \Theta(n^2) Θ(n2) algorithm on all inputs.
The number of key swaps is only Θ ( n ) \Theta(n) Θ(n) or more precisely n − 1 n−1 n−1 (one for each repetition of the i i i loop). This property distinguishes selection sort positively from many other sorting algorithms.
Insertion sort 插入排序
Worst-case: an array of strictly decreasing values
Best-case: already sorted arrays
Average-case:
Shellsort 希尔排序
Mergesort 归并排序
C ( n ) = 2 C ( n / 2 ) + C m e r g e ( n ) f o r n > 1 , C ( 1 ) = 0. C(n) = 2C(n/2) + C_{merge}(n) \space for\space n > 1, C(1) = 0. C(n)=2C(n/2)+Cme