9. Lower Bound on Comparison-based Sorting

通過見減少對比的次數(shù),我們可以提高算法的效率。

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.(設計算法一般考慮最壞情況)

我們發(fā)現(xiàn):
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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,113評論 0 23
  • 認識你之后,我才明白,這個世界上有些東西,不是你努力了就能擁有的。你總是說我很好,可惜,我還沒有好到讓你想要擁有。...
    安夏茉閱讀 908評論 120 27
  • 從未都坐在自己最舒適的椅子忘記了,站身給別人也坐一會兒。 年輕的真相是一心想追求自己遠大的理想,但與此同時也忽略了...
    旦真陳里閱讀 242評論 0 0
  • 這個世界在殘酷懲罰不改變的人 文by琉璃樹 這個標題是最近刷屏了深圳地鐵的廣告語,同一...
    琉璃樹I淺淺兮閱讀 10,288評論 54 74
  • 十年樹木 百年樹人 九十歲的他 還在作夢 問他夢是什么? 等得黃花菜都涼了 他還做著他的黃花夢 人生不滿百...
    淘猴侯孫行閱讀 212評論 3 7

友情鏈接更多精彩內(nèi)容