Introduction to Fuzzy sorting algorithm 7.6 C + + implementation code

Source: Internet
Author: User

The topic read a bit obscure, after understanding the thought is more easy to think. In fact, the order of the elements replaced by the closed interval, and the closed interval ' equal ' is that these intervals have a common area (equivalence class), so similar to exercise 7.2, return two subscript, they are equal elements, the front is the ' smaller ' interval, followed by the ' larger ' interval. So how to find the common sub-range, I here is the past to iterate over the array, if there is overlap, the main element is updated to overlapping areas, and then traverse, if the next interval and the main element overlap, then the interval and all the previous interval overlap.

In the second question, if n intervals have overlapping parts, that is, n elements are ' equal ', it is clear that only O (n) time is required, because the first division ends the sort.

#include <iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Interval;//determine if two intervals overlapBOOLIntersect (Interval i1, Interval i2) {returnI1.second >= I2.first&&i1.first <=I2.second;} Pair<int,int> interval_p (Interval *inv,intPintR) {    intPivot = p + rand ()% (R-p +1); Interval Temp=Inv[pivot];    Swap (Inv[pivot], inv[r]);  for(intK =0; K < R; ++k) {        if(Intersect (inv[k], temp)) {Temp.first=Max (Inv[k].first, Temp.first); Temp.second=min (Inv[k].second, temp.second); }    }    inti = P-1, j =p;  for(; J < R; + +)j) {        if(!Intersect (inv[j],temp))if(Inv[j].first <Temp.first) {                ++i;            Swap (Inv[j], inv[i]); }    }    intt = i+1;  for(j = i +1; J < R; ++j) {        if(Intersect (inv[j], temp)) {++i;        Swap (Inv[i], inv[j]); }} swap (Inv[i+1], inv[r]); return{T, i +1 };}//the input is an array subscriptvoidInterval_qs (Interval *inv,intPintR) {    if(P >=R)return; Pair<int,int> pa=interval_p (INV, P, R); Interval_qs (INV, p, Pa.first-1); Interval_qs (INV, Pa.second+1, R);}//Review the tail recursion, stack depth problemvoidTail_iqs (Interval *inv,intPintR) {     while(P <R) {Pair<int,int> PA =interval_p (INV, P, R); Tail_iqs (INV, p, Pa.first-1); P= Pa.second +1; }}intMain () {Interval inv[ A] = { {1,2}, {2,3}, {3,9}, {2,3}, {5,7}, {1,9}, {2,6}, {0,1}, {1,1}, {2,2}, {3,4}, {9,Ten} }; Tail_iqs (INV,0, One);  for(inti =0; I < A; ++i) cout<< Inv[i].first <<" "<< inv[i].second<<Endl;}

Introduction to Fuzzy sorting algorithm 7.6 C + + implementation code

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.