[Introduction to algorithms] Chapter 1 sorting Network

Source: Internet
Author: User

1. Compare Networks

1) Composition: Line and comparator


2) Meaning of the comparison Network: it is a set of comparator connected by lines. We draw a comparison network with N inputs into a graph composed of N horizontal lines, the comparator is vertically connected to two horizontal lines. The input end of each comparator is either A1, A2 ,......, One of them is connected, or it is connected to the output end of another comparator. Similarly, the output end of each comparator is either equivalent to N output lines B1, B2 ,......, One of BN is connected, or it is connected to the input end of another comparator. The interconnected comparator must meet the following requirements: no loop is required in the figure formed by the interconnection.

Only when there are two inputs at the same time can the comparator generate an output value.

Under the assumption that each comparator runs per unit time, we can define the "RunTime" of the comparison network, this is the time taken from the time when the input line receives its value to the time when all the output lines receive its value.

3) Compare Networks

4) sorting network: the output sequence of each input sequence increases monotonically (I .e. B1 <= b2 <=... <= Bn)Compare Networks

 5) 0-1 principle: if a comparison network with N inputs can correctly sort all the sequences of 2 ^ N 0 and 1 that may exist, then, the comparison network may sort all the sequences composed of any number correctly.

6)Double-tone sequence: sequence increases monotonically first, then monotonically, or monotonically, and then monotonically. For example, the sequence <,> and <9, 8,> are both double-tuned.

Ii. Two-Tone sorting Network

1) The double-tuned sorting program consists of 2n logstages, each of which is calledSemi-Cleaner. Each half-cleaner is a comparison network with a depth of 1, where the input line I is compared with the input line I + n/2, I = 1, 2 ,..., N/2 (n is assumed to be an even number ). Figure 4 is a semi-cleaner with eight inputs and Eight outputs.


When a two-tone sequence consisting of 0 and 1 is used as the input of the semi-cleaner, the output sequence generated by the semi-cleaner meets the following conditions:

1> smaller values are located in the upper half of the output, while larger values are located in the lower half of the output.

2> the two-part sequence is still double-tuned.

3> at least one of the two sequences is clean-all composed of 0 or 1. (The name is also derived from this)

2) process of generating the double-tone sorting Network

By recursively connecting the semi-cleaner, we can establish a double-tuned sorting network.Double-tuned sorting Network[N]The first phaseSemi-Cleaner[N]Composed by Theorem 1.Semi-Cleaner[N]Generates two half-scale double-tuning sequences, and each element that meets the upper half is no larger than any element in the lower half. Therefore, we can use twoDouble-tuned sorting Network[N/2]Recursively sorts the two parts to complete the sorting.


You only need to modify the first half cleaner of the double-tuned sorting network with n elements.Merge NetworksMerger [N]. Since the upper half and lower half of the input are monotonically increasing, we turn the lower half of the comparison network upside down and the input is a dual-tuning sequence. After adding a half cleaner, and then turning it back, the half cleaner is used to compare the input ai with an-I + 1. At this time, the output is also reversed. However, after a double-tuning sequence is reversed, it is still a double-tuning sequence.


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.