9. Lower Bound on Comparison-based Sorting

通过见减少对比的次数,我们可以提高算法的效率。

We can use a binary comparison tree to represent the sort procedure, where

  1. each node in the tree represents one comparison
  2. each node is labelled by the indices of the pair comparison elements
示意图

Important observations on the binary comparison tree of sorting:

  1. The number of nodes from the tree root to a tree leaf corresponds to the number of comparisons to sort a sequence
  2. There are n! tree leaves in the binary comparison tree, as there are
    n! = n·(n−1)·(n−2)·...·2·1 different sorted sequences for n elements to be sorted, depending on the n input element sequence.
  3. Minimizing the number of comparisons of sorting n elements is equivalent to minimizing the depth (or the height) of the binary comparison tree.

In other words, the problem becomes to construct a binary comparison tree that has n! leaves such that the longest path from the tree root to a tree leaf is minimized.(设计算法一般考虑最坏情况)

我们发现:
The depth of any binary tree containing no less than 2^h leaves is at least h − 1, assuming that the depth of the tree root is 1.

Thus, if a binary comparison tree contains N = n! leaves, then its minimum depth h is ⌈logN⌉,due to the fact that n!≥2^h andN=n!.

我们用二叉树可以证明,对比的最佳时间复杂度是nlogn。那么,optimal == 解决问题需要时间的下限 = 算法需要时间上限,该算法就可以当做最佳。

Question

(a) In the following definitions on the optimal algorithm for a problem P, which de- scription(s) is (are) correct, assuming that the problem size is n and its lower bound is n(n log log n)?

  1. Its running time is linear with the problem size n
  2. The lower bound of its running time is w(n log n)
  3. The lower bound of its running time is n(n log2 n)
  4. The upper bound of its running time is O(n2 log log n)

(b) In the following comparison-based sorting algorithms, (1) which ones are optimal algorithms and which ones are not?
• quick sort • heap sort • shell sort • bubble sort • merge sort • insertion sort

Answer: Heap and merge sort are optimal.

In the linear-time selection algorithm, the n elements in the input sequence are divided into groups of size 5 except the last group. Does the algorithm still run in time O(n) if the input elements are divided into groups of size 27 instead? Formulate the time complexity of the algorithm as a recurrence and solve the recurrence.


Assume that there is a comparison operator with a switch on it. The comparison operator can can take upto 3 elements as its input, its output is either the minimum value or the maximum value of the 3 elements, depending on the switch setting (e.g., setting the switch on, its output is the minimum value, otherwise (setting its switch off) its output is the minimum value).
If we count each comparison operator with its switch setting as one comparison. Given n >= 6 elements and assume that n is divisible by 6, what is the minimum number of comparisons needed in order to find the maximum and minimum values from these n elements? Can you propose an algorithm for it? Show the number of comparisons by your algorithm.


(b) Insert the keys 7,6,2,13,4,6,5,12 into a min-heap once a time, then remove the key in the root repeatedly until the heap is empty. Use diagrams to illustrate each step of the insertion and deletion procedure. What is the time complexity of sorting in this fashion?

Insertion
Extraction
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,484评论 0 23
  • 认识你之后,我才明白,这个世界上有些东西,不是你努力了就能拥有的。你总是说我很好,可惜,我还没有好到让你想要拥有。...
    安夏茉阅读 4,298评论 120 27
  • 从未都坐在自己最舒适的椅子忘记了,站身给别人也坐一会儿。 年轻的真相是一心想追求自己远大的理想,但与此同时也忽略了...
    旦真陈里阅读 1,345评论 0 0
  • 这个世界在残酷惩罚不改变的人 文by琉璃树 这个标题是最近刷屏了深圳地铁的广告语,同一...
    琉璃树I浅浅兮阅读 13,422评论 54 74
  • 十年树木 百年树人 九十岁的他 还在作梦 问他梦是什么? 等得黄花菜都凉了 他还做着他的黄花梦 人生不满百...
    淘猴侯孙行阅读 1,319评论 3 7