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
9. Lower Bound on Comparison-based Sorting_第1张图片
示意图

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.

9. Lower Bound on Comparison-based Sorting_第2张图片

9. Lower Bound on Comparison-based Sorting_第3张图片

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.

9. Lower Bound on Comparison-based Sorting_第4张图片

9. Lower Bound on Comparison-based Sorting_第5张图片

(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

你可能感兴趣的:(9. Lower Bound on Comparison-based Sorting)