Knowledge about the Sort function

Source: Internet
Author: User

Sort and Stable_sort

Need to include header file: #include <algorithm> because it is a library function

The principles of both functions are fast sorting, and the time complexity is the lowest in all sorts, O (nlog2n);

The application of sort;

1, can be passed two parameters;

Sort (a,a+n), where a is an array and a+n represents the N number of a[0] to a[n-1] (default from small to large);

2, pass in three parameters;

Sort (a,a+n,cmp), the third parameter is a function;

If the function from large to small sorting, can be implemented with the following algorithm;

BOOL CMP (int a,int b) {return a>b};

Sort (a,a+n,cmp);

and Stable_sort's usage is consistent with sort, and the difference is the Stable_sort function If you encounter two numbers equal, do not exchange the order This application is not affected in the array, and when the function parameter is passed into the structure, the obvious difference between the two is found;

Knowledge about the Sort function

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.