Comprehensible data Structure C language version (17)--Analysis of the sorting algorithm

Source: Internet
Author: User

In this post we will discuss some theorems related to sorting algorithms that will explain the questions raised in the Insert Sort blog post (why bubble sort and insert sort always do the same number of switching operations, and choose sort not necessarily), and pave the way for advanced sorting algorithms (why advanced Sorting is faster).

Before we discuss the relevant theorem, we must first grasp a concept related to order: reverse number .

The so-called reverse number, is the "number of reverse-order combination", assuming we want to be in a sequence from small to large (vice versa):

  There are elements of an inter-heterogeneous sequence x0,x1,x2 ... Xn-1,(element xor is not equal to any of the two numbers in the sequence) from which any two number is taken as a combination (XA,XB), if the A<b(that is, Xa in the sequence before Xb) and Xa>xb, then (XA,XB) reverse, if a< B and Xa<xb, then (XA,XB) is not in reverse order. The inverse number of the entire sequence is the number of all inverse combinations (XA,XB) in the sequence. (It is not difficult to find that if the sequence has n number, then any combination of two number (XA,XB) total n (n-1)/2, n is the number of elements. )

  

Let's give some examples of how to calculate the number of reverse orders (still assuming we want the order to be small to large):

1. There is a series of 1,2,3,4,5, because from any of the two (XA,XB) and a<b, there are XA<XB, that is, any combination (XA,XB) is not reverse order, so the number of reverse sequence 1,2,3,4,5 is: Reverse combination of the number =0

2. There is a series of 5,4,3,2,1, because from the two number (XA,XB) and a<b, all have xa>xb, that is, any combination (XA,XB) are reversed, so the number of reverse sequence 1,2,3,4,5 is: Reverse combination of the number =n (n-1)/2=5*4/2= 10

2. There are series 2,3,4,5,1, from which any two (XA,XB), a<b and XA>XB combination (2,1), (3,1), (4,1), (5,1) a total of 4, that is, the reverse combination of 4, so the series 2, The reverse number of 3,4,5,1 is: the number of reverse combinations =4

From Example 1 we can introduce Theorem 1: If the number column is ordered, then the number of reverseorder is 0. The computational process of Example 1 is the proof of this theorem.

And from the theorem 1 we can also draw the Theorem 2: If the sequence is not ordered, then its reverse number must be greater than 0. The proof process is similar to theorem 1, slightly

From Theorem 1 and theorem 2 we can draw the Theorem 3: The process of converting a sequence from a non-orderly to an ordered (that is, sort), is the process of reducing the number of reverse sequence numbers.

After understanding what the inverse number is, and the "essence" of the sorting algorithm, we begin to discuss theorem 4:

There are elements of an inter-heterogeneous sequence x0,x1,x2 ... Xn-1, whose inverse sequence is xn-1,xn-2,...... X0, the sum of the inverse numbers of these two series must be N (n-1)/2

Since the sequence elements are different, take two numbers XA,XB and a<b, either XA>XB or XA<XB. That is, if the combination (XA,XB) in the original sequence of non-reverse order, it will be reversed in reverse sequence, and vice versa. and the number of elements in the series of N (XA,XB) is a total of n (n-1)/2, either combination of the original sequence in reverse order, or reverse sequence, so the original sequence and inverse sequence number of the sum of the combination (XA,XB) Number: N (n-1)/2

With the theorem 4, we can give the theorem 5:

There are n different elements of the sequence, the average number of reverse order is n (n-1)/4

From theorem 4, it is known that the sum of the inverse number of the given sequence and its counter-sequence is n (n-1)/2, so the average number of reverse order of the sequence is half n (n-1)/2 of the sum of the two.

The following is the simple theorem 6:

For an element-XOR sequence, swapping the positions of the adjacent two-digit xn,xn+1, the number of reverse-order numbers in the sequence is either +1 or 1.

Assuming (xn,xn+1) is reversed, swapping both causes the combination to be non-inverted, but does not affect the order of the other combinations such as (XA,XN) (XN,XB), thus counting the number of columns in reverse-1

Assuming (xn,xn+1) is non-reversed, swapping both causes the combination to reverse, but does not affect the order of the other combinations such as (XA,XN) (XN,XB), thus the number of inverse numbers +1

Under

Theorem 3: The process of converting a sequence from a non-orderly to an orderly (that is, sort) is the process of reducing the number of inverse numbers up to 0.

Theorem 5: A sequence of n reciprocal elements with an average number of reverse order (N-1)/4

Theorem 6: For the elements of an inter-XOR sequence, exchange the position of the adjacent two number xn,xn+1, the number of reverse order of the sequence is either +1, or 1.

We can draw the theorem 7:

An algorithm that accomplishes sorting by exchanging adjacent elements with an average time complexity of Ω (N2)

For n-Element series, the average number of reverse order is n (n-1)/4, and the ordering is to reduce the number of reverse order to 0 of the process, because the exchange of adjacent elements up to reverse the number of 1, so there must be N (n-1)/4 exchanges in order to make the sequence number of 0, that is, the order of the Exchange N-1)/4 switching operation, i.e. the average time complexity is Ω (n (n-1)/4), or Ω (N2). While we have always emphasized that element differentiation may seem inappropriate, the sorting algorithm must be able to deal with element heterogeneity, and Ω (n) is only an average time complexity, without considering any specific situation.

   

At this point, this blog post for the sorting algorithm theorem is the introduction is complete. However, the analysis of the sorting algorithm has not been completed.

Let's start by answering the questions in the Insert Sort blog: Why bubble sort and insert sort perform the same number of interchange operations. the reasons are as follows:

Both of these algorithms are sorted by exchanging adjacent elements, and neither of them performs an exchange of +1 in the order of the number of columns, meaning that each time they perform an exchange, they will simply reverse the number of sequences-1. Therefore, for a given sequence number, if its reverse order is N, both will perform n exchange operations.

Then, the choice of sorting sometimes only requires a smaller number of times the reason is also out, because the choice of sorting is not by exchanging adjacent elements to sort the algorithm, such as sequence 5,4,3,2,1, select the first "real Exchange", the sequence becomes 1, 4,3,2,5, which means that this exchange makes the number of reverse order of the sequence-7. So choosing a sort can use less "real swapping" to complete the sorting, which does not mean that the choice of sorting will be quick, for reasons not to repeat.

Next, according to the preceding theorem (3, 5, 6). We can introduce the last theorem in this article, that is, the reasoning of Theorem 7, and the fundamental of all advanced sorting algorithms:

To make the sorting algorithm less time complex than O (N2), the algorithm must perform "long-distance element Exchange", which reduces the average number of reverse orders by more than 1.

  

In the next blog post we will discuss the hill sort, which is an upgraded version of the Insert Sort, where the upgrade is obviously "the exchange of elements far Away", and the idea of the upgrade is straightforward, and the implementation we will introduce in the next blog post.

Comprehensible data Structure C language version (17)--Analysis of the sorting algorithm

Related Article

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.