(a) Why use the sort function in the C + + standard library
The sort() function is one of the sorting methods of C + + . Learn this method also dispel I learn C + + since the use of bubble sorting and selection of sorting brought about the inefficient operation of the problem!
Because it uses the Sort method which is similar to the Fast platoon method, the time complexity is N*LOG2 (n), the operation 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. Just to show the results we want to be!
II)theSort function has three of the 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 from small to large, but also can not write a third parameter. The default sorting method at this point is from small to large.
The Sort function uses templates :
Sort (start,end, Sort method )
The following is a description of using the sort () function in detail with the 10 numbers in the array.
Example one: Thesort function does not have a third parameter. The realization is 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
With 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 thethird 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 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 in the complare function,// This is the rule
for (int i=0;i<10;i++)
cout<<a[i]<<endl;
return 0;
}
Example three:
This is a bit of a hassle, though, by using the method above in example one or two, although it is possible to sort from large to small and. Because of the need to write a function that tells the program what sort of order to run, the powerful features of theC + + 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
Combined with this example, this will complete whatever sort principle 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 be used to 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;
}
- /* This comparison function is very important. If you want to sort in ascending order, that is," < ", descending order is" > ", so it is easy to visualize. Suppose you want to use ELEM2 as a comparative standard
- Elem1 changed to ELEM2, so that the structure of the ELEM2 as a comparative standard sort. */ &NBSP; bool comparison (example a,example b) {
- return a.elem1<b.elem1;
- }
The sort function in C + +