Analysis on the usage of c++sort function

Source: Internet
Author: User

(a) Why use the sort function in the C + + standard library

The sort () function is one of the sorting methods of C + +, and this method also eliminates the problem of inefficient execution of the bubbling sorting and sorting that I used since I learned C + +! Because it uses the sorting method is similar to the fast line method, the time complexity is N*LOG2 (n), the execution efficiency is high!

(ii) Use of sorting functions in the C + + standard library

I) The sort function is included in the C + + standard library where the header file is #include<algorithm>, and calling the sorting method in the standard library does not have to know how it is implemented internally, as long as the results we want are possible!

II) The sort function has three parameters:

(1) The first one is the starting address of the array to sort.

(2) The second is the ending address (the last address to sort)

(3) The third parameter is the method of sorting, can be from large to small but also small to large, but also can not write the third parameter, the default sorting method is from small to large sort.

The sort function uses templates:

Sort (start,end, sort method)

Here's a description using the sort () function combined with the 10 numbers in the array!

Example one: the sort function does not have a third parameter, which is achieved from small to large

#include <iostream>

#include <algorithm>

using namespace Std;

int main ()

{

int a[10]={9,6,3,8,5,2,7,4,1,0};

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

Sort (a,a+10);

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

return 0;

}

Example Two

In the example above, there is a question: Do you want to do a big-to-small sort of swelling?

This is what you need to do in the third parameter in the sort () function, telling the program that I want to sort from the big to the small!

Need to add a comparison function complare (), the implementation of this function is like this

BOOL Complare (int a,int b)

{

Return a>b;

}

This is the way to tell the program to implement a sort from big to small!

#include <iostream>

#include <algorithm>

using namespace Std;

BOOL Complare (int a,int b)

{

Return a>b;

}

int main ()

{

int a[10]={9,6,3,8,5,2,7,4,1,0};

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

Sort (a,a+10,complare);//There is no need to pass parameters to the Complare function here,//This is the rule

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

return 0;

}

Example three:

Although the method of example one or two above is implemented from large to small and, it is still a bit cumbersome to do so, because it is also necessary to write a function that tells the program what sort principle to perform, and the powerful features of the C + + standard library can solve this problem completely.

The third parameter of the SORTT function can be used to tell the program what sort principle you are using

less< data type > ()//from small to large sort

greater< data type > ()//from large to small sort

With this example, you can do whatever sort of order you want.

#include <iostream>

#include <algorithm>

using namespace Std;

int main ()

{

int a[10]={9,6,3,8,5,2,7,4,1,0};

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

Sort (a,a+10,less<int> ());

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

return 0;

}

#include <iostream>

#include <algorithm>

using namespace Std;

int main ()

{

int a[10]={9,6,3,8,5,2,7,4,1,0};

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

Sort (a,a+10,greater<int> ());

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

return 0;

}

Example four: The use of the sort function can also achieve the sorting of characters, sorting method is similar, the following example shows the program

#include <iostream>

#include <algorithm>

using namespace Std;

int main ()

{

Char a[11]= "Asdfghjklk";

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

Sort (a,a+10,greater<char> ());

for (int i=0;i<10;i++)

cout<<a[i]<<endl;

return 0;

}

Analysis on the usage of c++sort function

Related Article

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.