DSAA之排序下界分析(一)

1. 回顾

  • In this section, we prove that any algorithm for sorting that uses only comparisons requires Ω ( n l o g n ) comparisons (and hence time) in the worst case, so that mergesort and heapsort are optimal to within a constant factor.
  • The proof can be extended to show that Ω ( n l o g n ) comparisons are required, even on average, for any sorting algorithm that uses only comparisons, which means that quicksort is optimal on average to within a constant factor.
      

  这里就应该回顾以前的知识了,上文强调了仅仅使用swap手段的排序算法,在最坏和平均情况下都需要 Ω ( n l o g n ) 次比较,这是个下界,也就是说至少需要 n l o g n 次比较。
  再次回顾,根据以前的记录篇,总结一下:

排序方法 最差情况 平均情况 最好情况
Quicksort O ( n 2 ) O ( n l o g n ) O ( n l o g n )
Mergesort O ( n l o g n ) O ( n l o g n ) O ( n l o g n )
Heapsort O ( n l o g n ) O ( n l o g n ) O ( n l o g n )
Insertsort O ( n 2 ) O ( n 2 ) O ( n )
Shellsort O ( n 2 ) o ( n 2 )
Radixsort O ( n ) O ( n ) O ( n ) 空间换时间

  以上表格,总结的了目前为止,笔者记录的几种排序的时间复杂度情况。希尔排序有些特殊,一些总结和笔者上面的时间复杂度有出入,可能是选用的增量不同的原因。

2. Decision Trees

Any sorting algorithm that uses only comparisons requires [ l o g n ! ] comparisons in the worst case and l o g n ! comparisons on average.

  以下DSAA的篇幅都在证明以上结论:

Each node represents a set of possible orderings, consistent with comparisons that have been made, among the elements. The results of the comparisons are the tree edges.如下图所示,决策树的概念比较简单。

这里写图片描述

  使用决策树的抽象概念是为了得到以下的结论:

  • Every algorithm that sorts by using only comparisons can be represented by a decision tree.
  • The number of comparisons used by the sorting algorithm is equal to the depth of the deepest leaf.最深树叶的深度其实就是整个树的高度
  • The average number of comparisons used is equal to the average depth of the leaves.平均比较次数,就是树的所有树叶的平均深度。
  • Let T be a binary tree of depth d. Then T has at most 2 d leaves.DSAA树的深度或者高度的是以路径长计数的,区别其他定义。
  • A binary tree with L leaves must have depth at least l o g L .上一点的反推,如果有L个树叶,那么至少应该有logL的深度。可以简单的假设下,2个叶节点至少树高为1。
  • Any binary tree with L leaves has an average depth of at least l o g L .
  • Any sorting algorithm that uses only comparisons between elements requires at least l o g n ! comparisons in the worst case.
    • A decision tree to sort n elements must have n ! leaves.
      所以n!的树叶,至少树高为logn!,那么最深树叶的高度至少为logn!,所以最少的比较次数为logn!
  • Any sorting algorithm that uses only comparisons between elements requires Ω ( n l o g n ) comparisons.根据上面的几点,无论是平均还是最坏情况,比较次数的下界都是logn

猜你喜欢

转载自blog.csdn.net/lovestackover/article/details/80428247