1 # include <iostream> 2 # include <ctime> 3 using namespace STD; 4 # define max (A, B) (A> B )? A: B 5 # define min (A, B) (A> B )? B: A 6 class interval 7 {8 Public: 9 double leftbound; 10 double rightbound; 11 interval (int A, int B): leftbound (A), rightbound (B) {} 12 interval (interval & I) {leftbound = I. leftbound; rightbound = I. rightbound;} 13 interval () {}14}; 15 void swap (interval & A, interval & B) 16 {17 interval temp = A; 18 A = B; 19 B = temp; 20 21} 22 void display (interval a [], int begin, int end) 23 {24 for (INT I = begin; I <= end; ++ I) 25 cout <"[" <A [I]. leftbound <"," <A [I]. rightbound <"]" <","; 26 cout <Endl; 27} 28 void partition (interval a [], int begin, int end, Int & INI, int & TER) // The INI and TER parameters are referenced to return the value 29 {30 31 interval begin = A [begin]; // The main component interval 32 ini = begin; 33 ter = end + 1; // note that ter starts from outside the array 34 35 int cur = begin + 1; 36 while (cur <TER & cur <End) 37 {38 if (a [cur]. rightbound <= found. leftbound & ini <End) // less than 39 {40 + ini; 41 swap (A [cur], a [INI]); 42 + + cur; 43 // cout <"1:"; 44 // display (A, begin, end); 45} 46 else if (a [cur]. leftbound> = middle. rightbound & TER> begin) // greater than the main unit interval 47 {48 -- ter; 49 swap (A [cur], a [ter]); 50 // cout <"2:"; 51 // display (A, begin, end); 52 53} 54 else // The same as the principal component interval, therefore, the intersection is used as the new principal component 55 {56 bytes. leftbound = max (comment. leftbound, a [cur]. leftbound); 57 seconds. rightbound = min (response. rightbound, a [cur]. rightbound); 58 // cout <"failed:" <strong. leftbound <"," <strong. rightbound <Endl; 59 + cur; 60 // cout <"3:"; 61 // display (A, begin, end ); 62} 63} 64 swap (A [INI], a [begin]); // adjust 65 66 -- ini; 67 // cout <"ini: "<ini <" <"ter:" <ter <Endl; 68 // display (A, begin, end ); 69 70 71} 72 void fuzzysortingofinterval (interval a [], int begin, int end) 73 {74 if (begin <End) 75 {76 int INI, ter; 77 partition (A, begin, end, INI, ter); 78 fuzzysortingofinterval (A, begin, INI); 79 fuzzysortingofinterval (A, Ter, end ); 80} 81} 82 int main () 83 {84 srand (Time (null); 85 const int size = 8; 86 interval A (28508,313 59 ), B (4712,304 66), C (23267,302 45), D (7134,809 8), E (25400,263 51), F (8079,290 52), g (31163,317 38 ), H (6346,243 52); 87 interval array [size] = {a, B, c, d, e, f, g, h }; 88 interval * array = new interval [size]; 89 for (INT I = 0; I <size; ++ I) 90 {91 array [I]. leftbound = rand () % 100; 92 array [I]. rightbound = rand () % 100; 93 while (array [I]. rightbound <array [I]. leftbound) 94 array [I]. rightbound = rand () % 100; 95} 96 display (array, 0, size-1); 97 fuzzysortingofinterval (array, 0, size-1); 98 display (array, 0, size-1); 99 system ("pause"); 100}
The algorithm is actually very simple. There are three comparison results for the interval: smaller than, equal to (having an intersection), greater than. The optimization here is, the intersection is used as the new principal component every time there is an equal case. The advantage is that the area equal to the center is expanded to reduce the recursive depth.
About the code, here we want to return a range of partition functions, which is to return numbers between the left and right, while C ++ can only return one value. Therefore, we use the technique of returning a reference, on the other hand, we can use the pair type of STL
pair<int,int>& partition(int begin,int end){ ..... return pair<int,int>(ini,ter);}
INI = partiton (begin, end). First; TER = partition (begin, end). Second;