2. Reverse comparison of thinking questions

Source: Internet
Author: User

only Merge Sorting needs to be modified.

 # include 
  
    # include 
   
     # include 
    
      # define infinite 1000 using namespace STD; int COUNT = 0; void input (vector 
     
       & V) {cout <"Please enter the number (<1000):" 
      
        data) v. push_back (data);} void Merge (vector 
       
         & V, int P, int Q, int R) {vector 
        
          V1, V2; int m = 0; int n = 0; For (INT I = P; I <= Q; I ++) v1.push _ back (v. at (I); v1.push _ back (infinite); For (INT I = q + 1; I <= r; I ++) v2.push _ back (v. at (I); v2.push _ back (infinite); For (INT I = P; I <= r; I ++) {If (V1 [m] <= v2 [N]) {v [I] = V1 [m]; m ++ ;} else {v [I] = v2 [N]; n ++; count + = (Q-P + 1-m); // count, because V1 and V2 are sorted, // once V1 [m]> V2 [N] appears, // the remaining elements in V1 are greater than V2 [N]. in this case, there are // Q-P + 1-m Reverse Order pairs }}} void mergesort (vector 
         
           & V, int P, int R) {If (P 
          
            V; input (V); mergesort (v, 0, V. size ()-1); cout 
           
          
         
        
       
      
     
    
   
  

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.