Sorting Algorithm of STL Algorithm

Source: Internet
Author: User

Sorting Algorithm of STL Algorithm

 

STL sorting algorithms are generally less complex than linear algorithms and must use random-access iterators.

Therefore, forward_list, list, associative and unordered contains do not provide random access iterators, and these containers cannot use sorting algorithms.

However, forward_list and list provide the member functions sort and associative contains for automatic sorting. unordered contains cannot be sorted.

Generally, sorting elements at a time is faster than keeping them in order. STL provides different sorting algorithms and selects suitable sorting algorithms as needed.

 

1. Sort all elements

1. The regular row is ordered.

Void

Sort (randomaccessiter beg, randomaccessiter end );

Use operator <to arrange elements in order. Quicksort is used in history, but introsort is used in since C ++ 11.

Introsort uses quicksort normally, but converts it to heapsort when the complexity is to square. Therefore,

The algorithm ensures the complexity of nlogn, which was previously an "average" complexity of nlogn.

// Change the sorting criterion:

Void

Sort (randomaccessiter beg, randomaccessiter end, binarypredicate OP );

Use OP (elem1, elem2); binary predicates as sorting criteria.

Note that op must be strictly weakly ordered, and the OP cannot change its status during function call.

 

. Maintain the relative order of equal elements

Void

Stable_sort (randomaccessiter beg, randomaccessiter end, [binarypredicate op]);

Compared with sort, the relative sequence of equal elements can be kept unchanged.

Based on mergesort, if there is enough external memory: the complexity of nlogn, otherwise, the complexity of N * logn.

 

Example:

 

2. segmented sorting: Keep some elements in order

Void

Partial_sort (randomaccessiter beg, randomaccessiter sortend,

Randomaccessiter end, [binarypredicate op]);

Feature: If all elements are not sorted, only [beg, sortend) is sorted. When this interval is ordered, the sorting is stopped.

If sortend = end, partial_sort () sorts all elements, the average performance is worse than sort (). Generally, the sorting time is twice that of sort (). But it has better performance in the worst case.

Generally, heapsort ensures the complexity of nlogn in any situation.

 

// Sort and copy versions at the same time:

 

3. sort by location: split into two parts

Void

Nth_element (randomaccessiter beg, randomaccessiter nth, randomaccessiter end, [binarypredicate op]);

Divide the interval into two parts: <= nth element and> = nth element. Or split it by OP (elem1, elem2.

Element <= any element in the second interval in the first interval.

It is applicable to finding n highest or lowest elements.

Linear complexity.

Because you do not know the exact sorting criterion for the last two intervals, the two parts may have the same value as nth.

 

4. sort by exact sorting criteria: Not a sorting algorithm, but a mutating algorithm. Therefore, random-access iterator is not required.

(1) move elements to the front end

Forwarditer

Partion (forwarditer beg, forwarditer end, unarypredicate OP );

Bidirectionaliter

Stable_partion (bidirectionaliter beg, bidirectionaliter end, unarypredicate OP );

Move all elements that meet OP (ELEM) = true to the front of the interval.

Stable_partion () maintains the relative order between elements of the matching criterion and the non-matching criterion.

When we also need to split the replication elements, we use partion_copy () to copy the elements matching the criterion to one destination range, and the elements that do not match the criterion to another range.

Complexity:

Partion (): linear.

Stable_partion (): There is enough memory, linear (numelems switching and call OP (); otherwise, nlogn (numelems call OP (), numelems * log (numelems) Switching ).

After the call, the number of elements in the first and second periods is unknown.

 

(2) split into two subintervals

Pari <outputiter1, outputiter2>

Partion_copy (inputiter sourcebeg, inputiter sourceend

Outputiter1 destturebeg, outputiter2 destfalsebeg,

Unarypredicate OP );

According to OP (), the satisfied part is placed in destturebeg, And the unsatisfied part is placed in destfalsebeg.

If you only need to copy the element that meets the predicate, use copy_if (). If you want to copy the part that does not meet the predicate, use remove_copy_if ().

Linear complexity.

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.