|
|
|
|
h = 1 while h < n, h = 3*h + 1 while h > 0, h = h / 3 for k = 1:h, insertion sort a[k:h:n] → invariant: each h-sub-array is sorted end
希尔排序的最坏情况下的时间复杂度依赖于增量序列。对于增量为 1 4 13 40 121..., 这是用在这里,时间复杂度为 O(n3/2)。对于其他的增量,时间复杂度是所知的O(n4/3),甚至O(n·lg2(n)),已知的既不是紧贴时间复杂度上界,也不是最好的增量序列。
由于希尔排序是基于插入排序,希尔排序继承插入排序的自适应性能。适应性不是剧烈的,因为希尔排序需要一次通过每个增量的数据,但它是显著的。对于递增序列如上图所示,有log3(n) 增量,所以接近排序完的数据的时间复杂度为O(n·log3(n))。
由于其低开销,相对简单的实现,自适应的性质,并分二次时间复杂度,希尔排序对于某些要进行排序的数据不是非常大的应用程序可能是一个可行的O(n·lg(n))排序算法的替代方案。
Algorithms in Java, Parts 1-4, 3rd edition by Robert Sedgewick. Addison Wesley, 2003.
Programming Pearls by Jon Bentley. Addison Wesley, 1986.
Quicksort is Optimal by Robert Sedgewick and Jon Bentley, Knuthfest, Stanford University, January, 2002.
Dual Pivot Quicksort: Code and Discussion.
Bubble-sort with Hungarian ("Csángó") folk dance YouTube video, created at Sapientia University, Tirgu Mures (Marosvásárhely), Romania.
Select-sort with Gypsy folk dance YouTube video, created at Sapientia University, Tirgu Mures (Marosvásárhely), Romania.
Sorting Out Sorting, Ronald M. Baecker with the assistance of David Sherman, 30 minute color sound film, Dynamic Graphics Project, University of Toronto, 1981. Excerpted and reprinted in SIGGRAPH Video Review 7, 1983. Distributed by Morgan Kaufmann, Publishers. Excerpt.