"Introduction to Algorithms" Sort (iv): Decision tree, linear time ordering (count, cardinality, bucket sort)

Source: Internet
Author: User

So far, the five major sorting algorithms have been summarized:

1. Insert Sort

2, bubble sort, select sort, Exchange sort (to classify these three methods as their ideas are essentially the same)

3. Merge sort

4, Heap sorting

5. Quick Sort

The above five sorts can be called "comparison sort", as the name suggests, because they are based on the comparison elements to determine their relative position.

The first two of the time are O (n^2), merge sort and heap sort worst O (n lg N), fast-row average O (n lg N)

Theorem: Any sort of comparison algorithm in the worst case, all need to do Ω (n lg N) times of comparison.

We prove by decision Tree:

Decision Trees (decision tree)

Comparison sorting can be abstracted as a decision tree. Scatter a decision tree is a tree full of two forks, indicating that a sort algorithm acts on all comparisons made by a given input element, while other factors are ignored.

Suppose there is a sequence of three elements, we use 1,2,3 to represent the three elements, we sort it based on comparisons, and we can have the following decision trees:

It is not difficult to find that the leaves of the decision tree represent all possible permutations of three elements.

In addition, you can always find a path to represent the entire comparison process by sorting the three elements in a comparison order. (Note that 1 does not mean that it represents the first element, it can represent either of the three elements.) 2,3 is the same. However, 1,2,3 does not point to the same element. Obviously the worst case complexity is the height of the decision tree.

For a decision tree with a height of H, which has an L-leaf node, it corresponds to a comparison order for n elements. Because n elements have n! Arrangement (arrangement of knowledge), each of which appears as a leaf in the book, so there are n!<=l (important, note:) and because in a high of H two-fork tree, the number of leaves not more than 2^h, then there are N! <= L <= 2^h

Take the logarithm of the equation and get:

H>=LG (n! ) (because the LG function is monotonically increasing)

=ω (n lg N)

Note: At first I thought it should be equal to the relationship, then Baidu a bit, the original text has this sentence: Because each of the N! Permutation appears as areachable leaf. The author's meaning is focused on using n! To represent all possible permutations of n elements, but all possible permutations of n elements are actually less than or equal to n! Because it is possible to have equal elements in n elements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.