Algorithm of STL sorting algorithm __

Source: Internet
Author: User
Tags function prototype

Previously did not pay attention to the sorting algorithm, because there are very few application scenarios, the recent interview process found that the sorting algorithm is often asked, sometimes in some written or interview topics need to use the sorting algorithm to solve the problem, if each of their own handwritten sort, then it is quite time-consuming, And I don't necessarily write out the wrong sort algorithm in a short time, this time STL provides the sorting algorithm is very important, and the STL sorting algorithm is each great God optimizes the result, I do not think I may write in the case which does not have the research to be able to compare with the STL to provide the algorithm to be comparable to the sort.

In short, learning the STL to our coding efficiency is very obvious, the West has a saying that "do not repeat the invention of the wheel", so learn how to use the existing wheels. You want to start mending the STL thing slightly.

Now to solve the immediate problem, through a blog to learn how to use the STL sorting algorithm. This is the text of the Portal 1 STL provided by the sort algorithm

C + + has been so many people like, because it has an object-oriented concept, but also maintain the high efficiency of C language features. The STL sorting algorithm also needs to be kept efficient. Therefore, for different needs, the STL provides different functions, different functions, the implementation of the algorithm is not the same. 1.1 Introduction to all sort algorithms

All the sort algorithm parameters need to be entered in a range, [begin, end]. The iterator (iterator) used here needs to be a random iterator (radomaccessiterator), that is, an iterator that can be randomly accessed, such as: It+n or something. (except partition and stable_partition)

If you need to define a comparison function yourself, you can pass the functor you have defined as a parameter. Each of these algorithms supports incoming comparison functions. The following is the list of names for all STL sort algorithm functions:

The
name of the function function Description
Sort Sort all the elements in a specified interval
Stable_sort Stable ordering of all elements in a specified interval
Partial_sort Partial ordering of all elements in a specified 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 whether an interval has been ordered.
Partition Make elements that meet a certain condition in the front
Stable_partition A relatively stable element in front of which a condition is met

Where Nth_element is the most difficult to understand, in fact, this function is used to find the first few. For example, to find the value of the number in the middle of an array that contains 7 elements, I may not care about the front or the back, and I only care about the value of the element in the fourth position. comparison functions in the 1.2 sort

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 equivalent
to calling sort (Vect.begin (), Vect.end (), less<int> ());

In the example above, the system itself provides a less imitation function for sort. Other affine functions are also available in the STL, and the following is a list of affine functions:

name function Description
Equal_to Equal
Not_equal_to Not equal
Less Less than
Greater Greater than
Less_equal Less than or equal to
Greater_equal Greater than or equal to

It should be noted that these functions are not all applicable to your sort algorithm, and how you choose it depends on your application. In addition, you cannot write the name of an imitation function directly, but rather write its overloaded () function:
Less ()
Greater ()
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 the < action symbol for overloaded types

#include <iostream> #include <algorithm> #include <functional> #include <vector> using

namespace Std;
        class MyClass {public:myclass (int a, int b): (a), second (b) {} int-i;
        int second;
        BOOL Operator < (const MyClass &m) Const {return i < M.first;

}
};

BOOL Less_second (const MyClass & M1, const MyClass & m2) {return m1.second < M2.second;}
        int main () {vector< MyClass > Vect;
                for (int i = 0; i < i + +) {MyClass my (10-i, i*3);
        Vect.push_back (my); for (int i = 0; i < vect.size (); i + +) cout<< "(<<vect[i].first<<," <<vect[i
        ].second<< ") \ n";
        Sort (Vect.begin (), Vect.end ());
        cout<< "After sorted by:" <<endl; for (int i = 0; i < vect.size (); i + +) cout<< "(" <<vect[i].first<< ", "<<vect[i].second<<") \ n ";
        cout<< "After sorted by second:" <<endl;
        Sort (Vect.begin (), Vect.end (), Less_second); for (int i = 0; i < vect.size (); i + +) cout<< "(<<vect[i].first<<," &LT;&LT;VECT[I].SECOND&L

        t;< ") \ n";
return 0; }
stability of 1.3 sort

You find that sort and stable_sort, and partition and Stable_partition, are surprised. The difference is that a function with stable guarantees that the original relative order of the equal elements remains unchanged after the order is sorted. Perhaps you will ask, since the equal, you also control him relative position, also don't know who is who. There's a problem here, and the equivalence here is that the function you provide means that two elements are equal, not necessarily the same element.
For example, if you write a comparison function:

BOOL Less_len (const string &STR1, const string &str2)
{return
        str1.length () < Str2.length ();
}

At this point, "Apple" and "winter" is equal, if the "Apple" appears in front of the "winter", with the stable function sorted, their order must be unchanged, if you are using a function without "stable" sort, then after sorting, " Winter "is probably in front of Apple." 1.4 Full Order

A full order arranges all elements of a given range in the order of size relationships. The functions used for full sorting are

Template <class randomaccessiterator>
void sort (Randomaccessiterator-I, Randomaccessiterator last);

Template <class Randomaccessiterator, class strictweakordering>
void sort (Randomaccessiterator-I, Randomaccessiterator last,
strictweakordering comp);

Template <class randomaccessiterator>
void Stable_sort (Randomaccessiterator-I, Randomaccessiterator last);

Template <class Randomaccessiterator, class strictweakordering>
void Stable_sort (randomaccessiterator Randomaccessiterator last, 
strictweakordering comp);

In the 1th, 3 forms, sort and stable_sort do not specify a comparison function, the system defaults to using operator< to sort all the elements within the interval [first,last), so if you use a type of rebel that has overloaded the operator < function, then you can worry about it. 2nd, 4 forms, you can specify the comparison function at will, the application is more flexible. Take a look at the actual application:
There are 10 students in the class, I want to know their grades.

#include <iostream> #include <algorithm> #include <functional> #include <vector> #include <

String> using namespace std;
        Class student{Public:student (const string &a, int b): Name (a), score (b) {} string name;
        int score;
        BOOL Operator < (const student &m) Const {return score< M.score;

}
};
        int main () {vector< student> vect;
        Student St1 ("Tom", 74);
        Vect.push_back (ST1);
        St1.name= "Jimy";
        st1.score=56;
        Vect.push_back (ST1);
        St1.name= "Mary";
        st1.score=92;
        Vect.push_back (ST1);
        St1.name= "Jessy";
        st1.score=85;
        Vect.push_back (ST1);
        St1.name= "Jone";
        st1.score=56;
        Vect.push_back (ST1);
        St1.name= "Bush";
        st1.score=52;
        Vect.push_back (ST1);
        St1.name= "Winter";
        st1.score=77;
        Vect.push_back (ST1);
    St1.name= "Andyer";    st1.score=63;
        Vect.push_back (ST1);
        St1.name= "Lily";
        st1.score=76;
        Vect.push_back (ST1);
        St1.name= "Maryia";
        st1.score=89;
        Vect.push_back (ST1);
        cout<< "------before sort ..." <<endl;
        for (int i = 0; i < vect.size (); i + +) cout<<vect[i].name<< ": T" <<vect[i].score<<endl;
        Stable_sort (Vect.begin (), Vect.end (),less<student> ());
        cout << "-----after sort ..." <<endl;
        for (int i = 0; i < vect.size (); i + +) cout<<vect[i].name<< ": T" <<vect[i].score<<endl;
return 0; }

Its output is:

------before sort ...
Tom:
jimy:
Mary:
Jessy: Jone: The
Bush:
winter:77
andyer:63
Lily:
maryia:89
-----After sort ....
Bush:
jimy:
jone:
andyer:63
Tom:
Lily:
winter:77
Jessy:
maryia:89
Mary:   92

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). Note 1, you can guarantee a good average performance, complexity is N*log (n), because the simple fast sort in theory has the worst, the performance is very low, its algorithm complexity is n*n, but most of the STL version has been done in this aspect of optimization, so you can be assured to use. 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. 1.5 Local Sort

Local sorting is actually a sort of ordering that is provided to reduce unnecessary operations. Its function prototype is:

template <class randomaccessiterator> void Partial_sort (RandomAccessIterator

Randomaccessiterator Middle, randomaccessiterator last);
Template <class Randomaccessiterator, class strictweakordering> void Partial_sort (Randomaccessiterator-A,

Randomaccessiterator Middle, Randomaccessiterator last, strictweakordering comp); Template <class Inputiterator, class randomaccessiterator> Randomaccessiterator partial_sort_copy (

Inputiterator, Inputiterator last, Randomaccessiterator Result_first, Randomaccessiterator result_last); Template <class Inputiterator, class Randomaccessiterator, class strictweakordering> Randomaccessiterator Partial_sort_copy (Inputiterator, Inputiterator last, Randomaccessiterator Result_first, RandomAccessIterator Result_last, Compare comp); 

After understanding the sort and stable_sort, it is easier to understand the partial_sort. First look at its use: There are 10 students in the class, I want to know who is the lowest score of 5. If you don't have partial_sort, you need to sort all the people in order, and then take the top 5. Now all you have to do is sort the minimum score of 5, and modify the above procedure as follows:

Stable_sort (Vect.begin (), Vect.end (),less<student> ());

To be replaced by:

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

The output results are:

------before sort ...
Tom:
jimy:
Mary:
Jessy: Jone: The
Bush:
winter:77
andyer:63
Lily:
maryia:89
-----After sort ....
Bush:
jimy:
jone:
andyer:63
Tom:
Mary:
Jessy:
winter:77
Lily:
maryia:89

Do you know the benefits of this? When the amount of data is small, may not see the advantage, if it is 1 million students, I want to find the lowest score of 5 people ...
Partial_sort uses the heap ordering (Heapsort), which in any case is N*log (n). If you want to use Partial_sort to achieve a full order, you just have to make middle=last.

Partial_sort_copy is actually a combination of copy and Partial_sort. The number of sorted (copied) is the smaller of [the Result_first, the result_last]. If the [Result_first, result_last) interval is greater than the [before] interval, then Partial_sort is equivalent to the combination of copy and sort. 1.6 Nth_element Specify the ordering of elements

Nth_element an easy to read but explain the more troublesome sort. It would be more convenient to use the example:
There are 10 students in the class, and I'd like to know the students who scored in the bottom 4th place.
If you want to meet the above requirements, you can use sort order, and then take the 4th position (because it is from small to large row), smarter friends will use Partial_sort, only the top 4, and then get the 4th place. In fact this is you or waste, because the top two you do not need to sort at all, at this time, you need to nth_element:

Template <class randomaccessiterator>
void nth_element (Randomaccessiterator-I, Randomaccessiterator Nth,
randomaccessiterator last);

Template <class Randomaccessiterator, class strictweakordering>
void Nth_element (randomaccessiterator Randomaccessiterator nth, Randomaccessiterator last
, strictweakordering comp);

For the above instance requirements, you only need to modify the program in 1.4 according to the following requirements:

Stable_sort (Vect.begin (), Vect.end (),less<student> ());

To be replaced by:

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

The results of the operation are:

------before sort ...
Tom:
jimy:
Mary:
Jessy: Jone: The
Bush:
winter:77
andyer:63
Lily:
maryia:89
-----After sort ....
Jone:
Bush:
jimy:
andyer:63
Jessy:
Mary:   92
winter:77
Tom:
Lily:
maryia:89

The fourth one is who. Andyer, this bad guy. Why is begin () +3 instead of +4? When I began to write this article, I did not care, and later in the ILOVEVC of the reminder, I found the problem. Begin () is the first, begin () +1 is the second, ... begin () +3 of course is the fourth. 1.7 partition and Stable_partition

As if these two functions were not used for sorting, the ' classification ' algorithm would be more appropriate. Partition is to divide the elements of an interval into two classes according to a certain condition. Its function prototype is:

Template <class ForwardIterator, class predicate>
forwarditerator partition (ForwardIterator
, ForwardIterator last, predicate pred);

template <class ForwardIterator, class predicate>
ForwardIterator stable_partition (ForwardIterator, ForwardIterator last, 
predicate pred);

Take a look at the application: 10 students in the class, counting all students who have not passed (below 60 points). You just need to replace the program in 1.4 with the following format:

Stable_sort (Vect.begin (), Vect.end (),less<student> ());

To be replaced by:

Student Exam ("pass");
Stable_partition (Vect.begin (), Vect.end (), bind2nd (less<student> (), exam));

The output is:

------before sort ...
Tom:
jimy:
Mary:
Jessy: Jone: The
Bush:
winter:77
andyer:63
Lily:
maryia:89
-----After sort ....
Jimy:
jone: The
Bush:
Tom:
Mary:
Jessy:  85
winter:77
andyer:63
Lily:
maryia:89

See, Jimy,jone, Bush (no wonder that the President of the United States is stupid smile) failed. And the use of stable_partition, the relative order between elements is unchanged. 2 Sort and Container

The main vectors of the standard containers in STL, List, deque, String, set, Multiset, map, Multimay, where set, Multiset, map, Multimap all store their elements in a tree-structured way. So in these containers, the elements are always ordered.
The iterator types of these containers are not random iterators, so the sort functions mentioned above are not available for these containers. The sort function above is available for the following containers:

Vector
string
deque

If your own defined container also supports random iterators, there is no problem using the sort algorithm.
for the list container, list comes with a sort member function List::sort (). It is similar to the sort in the algorithm function, but the List::sort is sorted based on the pointer, that is, all data movement and comparisons are implemented in the form of pointers, so that the sorted iterator remains in effect (the iterator of the sort in the vector is invalidated ). 3 Select the appropriate sort function

Why choose the right sort function. You may not care about efficiency (where the efficiency refers to the running time of the program), or the amount of data you have is small, so you think it doesn't matter which function you use.
In fact, even if you don't care about efficiency, if you choose the right sort of function, you will make your code easier to understand, you will make your code more scalable, and gradually develop a good habit, it is important to smile.

If you've ever used qsort in C, and you want to know qsort and their comparisons, I'll tell you, qsort and sort are the same, because they're all sort of fast. In terms of efficiency, the following sort algorithms are sorted, with efficiencies ranging from high to low (time-consuming from small to large):

Partion
stable_partition
nth_element
partial_sort
sort
stable_sort

Remember, the previous translation of the effective STL article, which summed up how to select the sorting function is good: if you want vector, string, deque, or array containers to complete the order, you can choose sort or stable_sort; String, deque, or array container to get the top n element, the partial sort partial_sort is preferred. For vectors, string, deque, or array containers, you need to find the nth position element or you need to get top N and the inner order of the N, which does not matter, nth_element is ideal; If you need to separate the elements that satisfy a condition or not satisfy a condition from a standard sequence container or array, you'd better use partition or stable_partition; If you use the list container, you can use partition and Stable_ directly. Partition algorithm, you can use List::sort instead of sort and stable_sort sorting. If you need to get a partial_sort or nth_element sort effect, you must use it indirectly. As described above, there are several ways to choose.
In short, remember a word: If you want to save time, do not detours, also do not go the extra road! 4 Summary

Discussion technology is like a bottomless pit, which can often be easily extended by a few other technical points. So we need to look at the problem from a global perspective, just like looking at the sort algorithm in the STL. In fact, there are make_heap, sort_heap and other sorting algorithms in STL. Not mentioned in this article. In this paper, an example is given to explain the characteristics of the sorting algorithm in STL, and how to choose the appropriate algorithm in the actual situation is summarized. 5 Reference Documentation

Clause 31: How to select a sort function
The Standard librarian:sorting in the Standard Library
Effective STL Chinese Version
Standard Template Library Programmer ' s Guide

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.