# The sorting algorithm in STL detailed parsing _c language

Source: Internet
Author: User

1. List of names for all STL sort algorithm functions:

Functional description of Function name

Sort sorts all the elements in a given interval

Stable_sort stable ordering of all elements in a given interval

Partial_sort the partial ordering of all elements in a given interval

Partial_sort_copy copy and sort for a given interval

Nth_element find the element that corresponds to a position in a given interval

Is_sorted to determine if an interval has been ordered.

Partition makes elements that meet a certain condition precede

Stable_partition is relatively stable so that elements that meet a certain condition are placed in the front

2. Comparison function:

When you need to sort in a particular way, you need to specify a comparison function for sort, or the program will automatically provide you with a comparison function

Vector < int > vect;

Sort (Vect.begin (), Vect.end ());//This is the equivalent of calling

Sort (Vect.begin (), Vect.end (), less<int> ());

Other comparison functions in sort

Equal_to equal

Not_equal_to Not Equal

Less less than

Greater greater than

Less_equal less than or equal

Greater_equal is greater than or equal to

In the example above, the system itself provides a less imitation function for sort. Other affine functions are provided in the STL, and the following is a list of affine functions: the name of the functor cannot be written directly, but its overloaded () function is written: less<int> ();

You can use these function templates directly when elements in your container are of standard type (int float char) or string. But if you define your own type or you need to sort by other means, there are two ways you can achieve the effect: one is to write the comparison function yourself. The other is an overloaded type of ' < ' operator assignment.

3. Full order:

A full order arranges all elements of a given range in the order of size relationships. Sort is a mature "fast sorting algorithm" (most of the current version of STL is not a simple quick sort, but a combination of interpolation sorting algorithm). The degree of complexity is N*log (n). Stable_sort uses the "merge sort", allocates enough memory, its algorithm complexity is N*log (n), otherwise its complexity is N*log (n) *log (n), its advantage is to maintain the relative position between equal elements in the order of consistency.

The functions used for full sorting are:

1.void sort (randomaccessiterator, randomaccessiterator last);

2.void sort (randomaccessiterator, randomaccessiterator last,strictweakordering comp);

3.void Stable_sort (Randomaccessiterator, Randomaccessiterator last);

4.void Stable_sort (Randomaccessiterator, Randomaccessiterator last, strictweakordering comp);

4. Local sort:

Partial_sort uses the heap ordering (Heapsort), which in any case is N*log (n).

Local sorting is actually a sort of ordering that is provided to reduce unnecessary operations.

Its function prototype is:

4.1 void Partial_sort (Randomaccessiterator, Randomaccessiterator middle,randomaccessiterator last);

4.2 void Partial_sort (Randomaccessiterator first,randomaccessiterator Middle,randomaccessiterator last, Strictweakordering comp);

4.3 randomaccessiterator partial_sort_copy (Inputiterator, Inputiteratorlast, Randomaccessiteratorresult_first,randomaccessiterator result_last);

4.4 Randomaccessiterator partial_sort_copy (Inputiterator, Inputiteratorlast, Randomaccessiteratorresult_first,randomaccessiterator Result_last, Compare comp);

For example: There are 1000 students in the class, I want to know who is the lowest score 5.

Partial_sort (Vect.begin (), Vect.begin () +5,vect.end (),less<student> ());

5. Nth_element Specify element ordering

5.1 void Nth_element (Randomaccessiterator, Randomaccessiterator nth, Randomaccessiterator last);

5.2 void Nth_element (Randomaccessiterator, Randomaccessiterator Nth,randomaccessiterator last,

Strictweakordering comp);

For example: There are 1000 students in the class, and I'd like to know the students who scored in the bottom 4th place.

Nth_element (Vect.begin (), Vect.begin () +3, Vect.end (),less<student> ());

6. Partition and Stable_partition

Partition is to divide the elements of an interval into two classes according to a certain condition, and there is no sort.

Its function prototype is:

6.1 ForwardIterator partition (ForwardIterator, forwarditerator, predicate)

6.2 ForwardIterator stable_partition (ForwardIterator, forwarditerator, predicate);

For example: 10 students in the class, calculating all students who have not passed (below 60 points):

Student Exam ("pass", 60);

Stable_partition (Vect.begin (), Vect.end (), bind2nd (less<student> (), exam));

7. Efficiency from high to low (time-consuming from small to large):

Partion

Stable_partition

Nth_element

Partial_sort

Sort

Stable_sort

8. The effective STL summarizes how to select the Sort function:

8.1 You can choose sort or stable_sort if you want to sort the vector, string, deque, or array container in a full order.

8.2 If you only need to get top n elements in a vector, string, deque, or array container, the partial sort partial_sort is preferred.

8.3 in the case of vectors, string, deque, or array containers, you need to find the nth position element or you need to get top N and not the inner order of the highest n, Nth_element is ideal;

8.4 If you need to separate from the standard sequence container or array the elements that satisfy a condition or not satisfy a condition, you'd better use partition or stable_partition;

8.5 If you use the list container, you can use the partition and Stable_partition algorithms directly, you can use List::sort instead of sort and stable_sort sorting.

Related Keywords:

### 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.