1.7 Reverse Order Number and merge order [inversion pairs by merge sort]

Source: Internet
Author: User

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/inversion-pairs-by-merge-sort.html

[Question]

With the beauty of programming, the issue of cutting light and shade can be further transformed into reverse order.

[Analysis]

Solving the reverse order problem is similar to MergeSort. You only need to make a slight modification to MergeSort. MergeSort adopts the division and control method. If you need to sort A [p... r], then it can be divided into A [p... q] And A [q... r], and then the two parts of the ordered Merge, and the Merge process is the time complexity of round (n. Based on the primary fixed rate T (n) = 2 (Tn/2) + hour (n), the time complexity is T (n) = hour (nlgn ).

Similarly, the idea of division and control can also be used to find the reverse order of the entire sequence, that is

Reverse Order (A [p... r]) = Reverse Order (A [p... q]) + reverse order (A [q... r]) + reverse order (A [p... q], A [q... r).

In combination with MergeSort, the key is how to effectively find the reverse order between A [p... q] And A [q... r] at the given (n) time. Because in the Merge process of merging and sorting, A [p... q] And A [q... r] has been ordered. Assume that Merge has been directed to A [I... q] And A [j... r]. Consider the next step: If A [I] <= A [j], it means that A [I] is better than the subsequent sequence A [j... the elements in r] are small and there is no reverse order. If A [I]> A [j], it means that A [j] is better than the previous sequence A [I... q] All are small, that is, the number of Reverse Order pairs ending with j is the number of remaining Series A [I... q] Number of elements.

In the Merge process, A [p... r], A [r... q] The number of Reverse Order pairs between them. The time complexity is also limit (n), which is the sumTime Complexity: hour (nlgn)This method is much better than the simple method round (n * n.

[MergeSort]

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/25
*/
Void merge (int * A, int p, int q, int r)
{
// Li: p... q Rj: q + 1... r
Int n1 = q-p + 1;
Int n2 = r-(q + 1) + 1;
Int * L = new int [n1 + 1];
Int * R = new int [n2 + 1];
// Copy L and R
For (int I = 0; I <n1; I ++)
L [I] = A [p + I];
For (int j = 0; j <n2; j ++)
R [I] = A [q + 1 + j];
// Mark end
L [n1] = INT_MAX;
R [n2] = INT_MAX;
Int I = 0; // left
Int j = 0; // right
Int k = 0; // whole
For (k = p; k <= r; k ++)
{
If (L [I] <= R [j])
{
A [k] = L [I];
I ++;
}
Else
{
// L [I]> R [j]
A [k] = R [j];
J ++;
}
}

Delete [] L;
Delete [] R;
}

Void merge_sort (int * A, int p, int r)
{
If (p <r)
{
Int q = (p + r)/2;
Merge_sort (A, p, q );
Merge_sort (A, q + 1, r );
Merge (A, p, q, r );
}
}

Void MergeSort (int * A, int n)
{
Merge_sort (A, 0, n-1 );
}

[InversionPair]

InversionPair only needs to add three lines of code records to the merge function. First initialize inversion_pairs to 0, when L [I]> R [j], update inversion_pairs = inversion_pairs + (n1-i), and finally return. Return the sum of left_pair, right_pair, and corss_pair in merge_sort.

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/25
*/
Int merge_pair (int * A, int p, int q, int r)
{
// Li: p... q Rj: q + 1... r
Int n1 = q-p + 1;
Int n2 = r-(q + 1) + 1;
Int * L = new int [n1 + 1];
Int * R = new int [n2 + 1];
// Copy L and R
For (int I = 0; I <n1; I ++)
L [I] = A [p + I];
For (int j = 0; j <n2; j ++)
R [I] = A [q + 1 + j];
// Mark end
L [n1] = INT_MAX;
R [n2] = INT_MAX;
Int I = 0; // left
Int j = 0; // right
Int k = 0; // whole
// ==================================
Int inversion_pairs = 0;
// ==================================
For (k = p; k <= r; k ++)
{
If (L [I] <= R [j])
{
A [k] = L [I];
I ++;
}
Else
{
// L [I]> R [j]
A [k] = R [j];
J ++;
// ==================================
Inversion_pairs + = (n1-I );
// ==================================
}
}

Delete [] L;
Delete [] R;
// ==================================
Return inversion_pairs;
// ==================================
}

Int inversion_pair (int * A, int p, int r)
{
If (p <r)
{
Int q = (p + r)/2;
// ================================================
Int left_pair = inversion_pair (A, p, q );
Int right_pair = inversion_pair (A, q + 1, r );
Int cross_pair = merge_pair (A, p, q, r );
Return left_pair + right_pair + cross_pair;
// ================================================
}
Else
Return 0;
}

Int InversionPair (int * A, int n)
{
Return inversion_pair (A, 0, n-1 );
}

[LINK]

Http://www.cnblogs.com/bovine/archive/2011/09/22/2185006.html

Http://blog.csdn.net/zhanglei8893/article/details/6230233

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/inversion-pairs-by-merge-sort.html

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.