Sort function Array and string sorting __ algorithm

Source: Internet
Author: User

(a) Why use the Sort function in C + + standard library
The sort () function is one of the sort methods in C + +, which uses the sort method similar to the fast row method, the time complexity is N*LOG2 (n), and the execution efficiency is high.
(b) The use of sorting functions in C + + standard libraries
(1) 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 the interior is implemented, as long as the results we want are available.
(2) The sort function has three parameters:
1. The first is the starting address of the array to be sorted.
2. The second is the ending address (the last address to be sorted).
3. The third parameter is a sort of method, can be from small to large but also from large to small, you can not write a third parameter, at this time the default sorting method is from small to large sort.
The sort function uses a template:
Sort (start,end, sorting method)
The following is a description using the Sort function in conjunction with the 10 numbers in the array.
Example one: the sort function does not have a third parameter and realizes a small to large

#include <iostream>
#include <algorithm>
using namespace std;
int main ()
{
    int arr[10] = {8,5,7,3,9,2,1,0,6,4};
    cout << "Order before ordering:";
    for (int i = 0;i < 10;i++)
    {
        cout << arr[i] << "";    Output sort before order
    }
    cout << Endl;
    Sort (arr, arr +);
    cout << "sorted after order:";
    for (int i = 0;i < 10;i++)
    {
        cout << arr[i] << "";   Output sorted after order
    }
    cout << Endl;
    return 0;
}

Output results:

Example two: Set the third parameter to achieve a sort from large to small
Through the example above, there is a question: what to do if you want to achieve a large to small sort. This is what you need to do in the third argument in the sort function, telling the program I want to sort from big to small. You need to add a comparison function compare, which is the implementation of this function.

BOOL Compare (int a, int b)      //define collation
{return
    a > b;               Sort from large to small
}

This is the way to tell the program to achieve a sort from large to small.

#include <iostream>
#include <algorithm>
using namespace std;
BOOL Compare (int a, int b)        //define collation
{return
    a > b;                 From large to small sort
}
int main ()
{
    int arr[10] = {8,5,7,3,9,2,1,0,6,4};
    cout << "Order before ordering:";
    for (int i = 0;i < 10;i++)
    {
        cout << arr[i] << "";    Output sort before order
    }
    cout << Endl;
    Sort (arr, arr + 10,compare);
    cout << "sorted after order:";
    for (int i = 0;i < 10;i++)
    {
        cout << arr[i] << "";    Output sorted after order
    }
    cout << Endl;
    return 0;
}

Output results:

Example three: Using the sort function to sort strings from small to large

#include <iostream>
#include <algorithm>
using namespace std;
int main ()
{
    char str[10] = "ADEBCHGFI";
    cout << "Order before ordering:";
    for (int i = 0;i < 9;i++)
    {
        cout << str[i];     Output sort before order
    }
    cout << Endl;   
    Sort (str, str+ 9);
    cout << "sorted after order:";
    for (int i = 0;i < 9;i++)
    {
        cout << str[i];     Output sorted after order
    }
    cout << Endl;
    return 0;
}

Output results:

Example four: sorting strings from large to small using the sort function

#include <iostream>
#include <algorithm>
using namespace std;
BOOL Compare (char A, char b)               //Add collation
{return 
    a > b;
}
int main ()
{
    char str[10] = "ADEBCHGFI";
    cout << "Order before ordering:";
    for (int i = 0;i < 9;i++)
    {
        cout << str[i];            Output sort before order
    } 
    cout << Endl;   
    Sort (str, str+ 9,compare);       Call collation
cout << "Sort after order:";
    for (int i = 0;i < 9;i++)
    {
        cout << str[i];            Output sorted after order
    }
    cout << Endl;
    return 0;
}

Output results:

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.