Sorting Algorithm of STL

Source: Internet
Author: User

 Sort Sort elements in range (function template) Stable_sort Sort elements preserving order of equivalents (function template) Partial_sort Partially sort elements in range (function template) Partial_sort_copy Copy and partially sort range (function template) Nth_element Sort element in range (function template)

Among them, sort, and stable_sort have appeared in many of my blogs and are no longer cumbersome here. If you want to review sort and stable_sort, you can refer to my blog

1partial_sort

`template <class RandomAccessIterator>  void partial_sort ( RandomAccessIterator first, RandomAccessIterator middle,                      RandomAccessIterator last );template <class RandomAccessIterator, class Compare>  void partial_sort ( RandomAccessIterator first, RandomAccessIterator middle,                      RandomAccessIterator last, Compare comp );`

The above is the prototype of its function, which is based on the intermediate address you give, and then partially sorted, for example, the address you give is v. begin (), V. begin () + 5, V. end (), where the value of V is 10, then the result is sorted by the first five, and the last five are sorted by the original order.

`#include<iostream>#include<string>#include<vector>#include<algorithm>#include<functional>using namespace std;int main(){int a[]={10,9,8,7,6,5,4,3,2,1};vector<int>v(a,a+10);partial_sort(v.begin(),v.begin()+5,v.end());for(vector<int>::iterator itera=v.begin();itera!=v.end();++itera){cout<<*itera<<" ";}cout<<endl;system("pause");return 0;}`
`#include<iostream>#include<string>#include<vector>#include<algorithm>#include<functional>using namespace std;bool isBigger(int a,int b){return a>b;}int main(){int a[]={1,2,3,4,5,6,7,8,9,10};vector<int>v(a,a+10);partial_sort(v.begin(),v.begin()+5,v.end(),isBigger);for(vector<int>::iterator itera=v.begin();itera!=v.end();++itera){cout<<*itera<<" ";}cout<<endl;system("pause");return 0;}`

The code is simple. You don't have to explain it.

2partial_sort_copy,

The function prototype is as follows:

`template <class InputIterator, class RandomAccessIterator>  RandomAccessIterator    partial_sort_copy ( InputIterator first,InputIterator last,                        RandomAccessIterator result_first,                        RandomAccessIterator result_last );template <class InputIterator, class RandomAccessIterator, class Compare>  RandomAccessIterator    partial_sort_copy ( InputIterator first,InputIterator last,                        RandomAccessIterator result_first,                        RandomAccessIterator result_last, Compare comp );`

In fact, it is equivalent to the combination of sort () and copy. The so-called partial does not have a given range. In fact, it uses a lot of data, or begin ---> end

Check the Code:

`#include<iostream>#include<string>#include<algorithm>#include<functional>#include<vector>using namespace std;int main(){int a[]={4,5,2,1,3,6,9,8,7,2};vector<int>v(a,a+10);vector<int>vv(10);partial_sort_copy(v.begin(),v.end(),vv.begin(),vv.end());for(vector<int>::iterator itera=vv.begin(); itera!=vv.end();++itera){cout<<*itera<<" ";}cout<<endl;system("pause");return 0;}`
`#include<iostream>#include<string>#include<algorithm>#include<functional>#include<vector>using namespace std;bool isBigger(int a,int b){return a>b;}int main(){int a[]={4,5,2,1,3,6,9,8,7,2};vector<int>v(a,a+10);vector<int>vv(10);partial_sort_copy(v.begin(),v.end(),vv.begin(),vv.end(),isBigger);for(vector<int>::iterator itera=vv.begin(); itera!=vv.end();++itera){cout<<*itera<<" ";}cout<<endl;system("pause");return 0;}`

Use of the nth_element () method in STL by calling nth_element (start,
The START + N, end) method can place the nth element at the nth position (starting from 0, where the subscript is N ), in addition, elements smaller than this element are placed before this element. Elements larger than this element are placed behind this element, but they cannot be ordered.

The function prototype is as follows:

`template <class RandomAccessIterator>  void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,                     RandomAccessIterator last );template <class RandomAccessIterator, class Compare>  void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,                     RandomAccessIterator last, Compare comp );`

Check the Code:

`#include<iostream>#include<string>#include<vector>#include<algorithm>#include<functional>using namespace std;int main(){int a[]={10,4,2,7,6,5,9,3,8,1};vector<int>v(a,a+10);for(vector<int>::iterator itera=v.begin();itera!=v.end();++itera){cout<<*itera<<" ";}cout<<endl;nth_element(v.begin(),v.begin()+6,v.end());for(vector<int>::iterator iterb=v.begin();iterb!=v.end();++iterb){cout<<*iterb<<" ";}cout<<endl;system("pause");return 0;}`

Related Keywords:

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.