STL source code profiling algorithm stl_algo.h -- merge sort

Source: Internet
Author: User

STL source code profiling algorithm stl_algo.h -- merge sort

 

----------------------------------------------------------------------

 


Description: Merge Sorting.
Ideas:
1. Semi-segmentation of intervals
2. Sort the Left and Right segments separately
3. Use inplace_merge to merge left and right segments into a complete ordered sequence


Complexity: O (nlog n)
Source code:
Template

 
  
Void mergesort (BidirectionalIter first, BidirectionalIter last) {typename iterator_traits

  
   
: Diference_type n = distance (first, last); if (n = 0 | n = 1) return; else {BidirectionalIter mid = first + n/2; mergesort (first, mid); mergesort (mid, last); inplace_merge (first, mid, last );}}

  

 


Example:

 

Int main () {int a [] = {, 9, 9 }; int * a_end = a + sizeof a/sizeof (int); std: cout <''; mergesort (a, a_end); std :: cout <); std: cout <''; return 0 ;}


 

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.