Introduction to algorithms Chapter 1 after-school questions (Fuzzy sorting of intervals)

Source: Internet
Author: User
I. Question

Consider such a sorting problem, that is, you cannot accurately know the exact size of each number in the sorting. for each number, we only know that it falls within a certain range on the real axis. that is, the given n closed intervals in the form of [ai, BI], where Ai is less than or equal to bi. the goal of the algorithm is to perform fuzzy-sort for these intervals, that is, to generate a sort for each interval. <I1,
I2, I3, I4 ,... In>, so that there is a CJ in [ai,
BI], satisfying C1 ≤ C2 ≤... ≤ CN.)           Design an algorithm for Fuzzy sorting of N intervals. Your algorithm should have a general structure of the algorithm, which can quickly sort the left endpoint (that is, each AI To improve the running time. (as the number of overlapping intervals increases, sorting of intervals becomes more and more easy. Your algorithm should be able to make full use of this overlap .) b)         Proof:
Generally, your algorithm runs at O (N * lgn) intervals, but when all the intervals overlap, the expected run time is O (n) (that is, when there is a value of X, so that all I have X in [ai, BI]). your algorithms should not explicitly check this situation, but should naturally improve performance as the number of overlaps increases.

Ii. Thinking

Based on the Division idea of the Quick Sort, a certain element is used as the primary element to divide the region into three segments. The first segment is smaller than the principal component, and the second segment is equal to the principal component.

The focus is on Division. If the intervals overlap, they will be treated as equal, and the public part will be extracted for further division.

A. End <B. Start => A <B

A. Start> B. End => A> B

Other cases => A = B

To avoid situations such as (), We need to extract the common factor and update the principal element when the principal element is equal)

The specific explanations are in the code.


Iii. Code

[CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace STD;
  3. Struct Node
  4. {
  5. Int start;
  6. Int end;
  7. Bool operator <(const node & B) const
  8. {
  9. Return end <B. Start;
  10. }
  11. Bool operator = (const node & B) const
  12. {
  13. Return (end> = B. Start) & (start <= B. End );
  14. }
  15. Bool operator> (const node & B) const
  16. {
  17. Return Start> B. end;
  18. }
  19. };
  20. // Division results: 0-> A is smaller than the principal component, A + 1-> B-1 is equal to the principal component, B-> length_a is greater than the principal component
  21. Struct divid
  22. {
  23. Int;
  24. Int B;
  25. };
  26. Node A [11];
  27. Int length_a = 10;
  28. // Display the results in three rows
  29. Void print (divid D)
  30. {
  31. Int I = 1;
  32. If (D. A> 0)
  33. {
  34. For (I = 1; I <= d. a; I ++)
  35. Cout <'(' <A [I]. Start <',' <A [I]. End <")";
  36. Cout <Endl;
  37. I = D. A + 1;
  38. }
  39. If (d. B> 0)
  40. {
  41. For (; I <D. B; I ++)
  42. Cout <'(' <A [I]. Start <',' <A [I]. End <")";
  43. Cout <Endl;
  44. I = D. B;
  45. }
  46. If (I <= length_a)
  47. {
  48. For (; I <= length_a; I ++)
  49. Cout <'(' <A [I]. Start <',' <A [I]. End <")";
  50. Cout <Endl;
  51. }
  52. Cout <Endl;
  53. }
  54. // Exchange
  55. Void Exchange (node & A, node & B)
  56. {
  57. Node temp;
  58. Temp =;
  59. A = B;
  60. B = temp;
  61. }
  62. // Division is important
  63. Divid partition (node * a, int P, int R)
  64. {
  65. // Take any element as the primary element
  66. Node x = A [R];
  67. Int I = p-1, j = R + 1, K = P;
  68. While (k <= R & K <j)
  69. {
  70. // If it is less than the principal component, switch to the front
  71. If (A [k] <X)
  72. {
  73. I ++;
  74. Exchange (A [I], a [k]);
  75. K ++;
  76. }
  77. // If the value is greater than, it is switched to the end.
  78. Else if (a [k]> X)
  79. {
  80. J --;
  81. Exchange (A [J], a [k]);
  82. // K ++ is not allowed here, because the exchanged elements may be greater than the principal element.
  83. }
  84. Else
  85. {
  86. // If they are equal, they are not exchanged, but the common factor must be extracted.
  87. X. End = min (X. End, a [K]. End );
  88. X. Start = max (X. Start, a [K]. Start );
  89. K ++;
  90. }
  91. }
  92. // Return the division result
  93. Divid ret = {I, j };
  94. If (Ret. A <p) ret. A =-1;
  95. If (Ret. B> r) ret. B =-1;
  96. Print (RET );
  97. Return ret;
  98. }
  99. Void quicksort (node * a, int P, int R)
  100. {
  101. If (P> = r)
  102. Return;
  103. // Divide the array into three segments
  104. Divid q = partition (A, P, R );
  105. // If the first segment exists, sort the first segment
  106. If (Q. A> 0)
  107. Quicksort (A, p, q. );
  108. // If the third segment exists, sort the third segment
  109. If (Q. B> 0)
  110. Quicksort (A, Q. B, R );
  111. }
  112. Int main ()
  113. {
  114. Int I, N;
  115. Cin> N;
  116. Length_a = N;
  117. // Init data by random
  118. For (I = 1; I <= length_a; I ++)
  119. {
  120. A [I]. Start = rand () % 100;
  121. A [I]. End = rand () % 100;
  122. If (A [I]. Start> A [I]. End)
  123. Swap (A [I]. Start, a [I]. End );
  124. }
  125. Divid d = {-1,-1 };
  126. Print (d );
  127. // Sort
  128. Quicksort (A, 1, length_a );
  129. Return 0;
  130. }
# Include <iostream> using namespace STD; struct node {int start; int end; bool operator <(const node & B) const {return end <B. start;} bool operator = (const node & B) const {return (end> = B. start) & (start <= B. end);} bool operator> (const node & B) const {return start> B. end ;}}; // partition result: 0-> A is smaller than principal component, A + 1-> B-1 is equal to Principal Component, B-> length_a is greater than Principal Component struct divid {int; int B ;}; node A [11]; int length_a = 10; // display void print (divid d) {int I = 1; if (D. a> 0) {for (I = 1; I <= D. a; I ++) cout <'(' <A [I]. start <',' <A [I]. end <")"; cout <Endl; I = D. A + 1;} If (D. b> 0) {for (; I <D. b; I ++) cout <'(' <A [I]. start <',' <A [I]. end <")"; cout <Endl; I = D. b;} if (I <= length_a) {for (; I <= length_a; I ++) cout <'<A [I]. start <',' <A [I]. end <")"; cout <Endl ;}cout <Endl ;}// exchange void Exchange (node & A, node & B) {node temp; temp = A; A = B; B = temp;} // Division is the key divid partition (node * a, int P, int R) {// take any element as the primary node x = A [R]; int I = p-1, j = R + 1, K = P; while (k <= R & K <j) {// if it is smaller than the principal component, switch to the front if (a [k] <X) {I ++; exchange (A [I], a [k]); k ++;} // if the value is greater than, switch to the following else if (a [k]> X) {J --; exchange (A [J], a [k]); // here K ++ is not allowed, because the exchanged elements may be greater than the principal element} else {// if they are equal, do not exchange, but extract the public factor X. end = min (X. end, a [K]. end); X. start = max (X. start, a [K]. start); k ++; }}// returns the division result divid ret = {I, j}; If (Ret. A <p) ret. A =-1; if (Ret. b> r) ret. B =-1; print (RET); return ret;} void quicksort (node * a, int P, int R) {If (P> = r) return; // divide the array into three segments: divid q = partition (A, P, R); // if the first segment exists, sort the first segment if (Q. a> 0) quicksort (A, P, Q. a); // if the third segment exists, sort the IF (Q. b> 0) quicksort (A, Q. b, R) ;}int main () {int I, n; CIN> N; length_a = N; // init data by randomfor (I = 1; I <= length_a; I ++) {A [I]. start = rand () % 100; A [I]. end = rand () % 100; if (a [I]. start> A [I]. end) Swap (A [I]. start, a [I]. end) ;}divid D ={-1,-1 }; print (d); // sortquicksort (A, 1, length_a); Return 0 ;}

 

Reprinted from: http://blog.csdn.net/mishifangxiangdefeng/article/details/7681109

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.