Sorting algorithm accumulation (2)

Source: Internet
Author: User

Reprint: http://blog.csdn.net/sunshangjin/article/details/40296357

Think of themselves every day sort of sort, bubbling ah, binary search Ah, the results in the STL with the sorting function Sort,qsort, finally freed himself ~

The Std::sort () function is powerful and can be used to sort elements such as classes, structs, and so on. C++sort Reference Manual

So I summed up a bit, first look at the sort function in the following table: Reference csdn

Name of function function Description
Sort Sort all elements of a given interval
Stable_sort Stable ordering of all elements in a given interval
Partial_sort Sort all elements of a given interval
Partial_sort_copy Copy and sort the given interval
Nth_element Find the element corresponding to a position in a given interval
is_sorted Determine if an interval has been sequenced.
Partition Put elements that meet a certain condition in front
Stable_partition Relatively stable so that elements that meet a certain condition are placed in front

To use this function just use # include <algorithm>

Sort can be used with the syntax described as:

Sort (begin,end), which represents a range, for example:

[CPP]View PlainCopy
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. Using namespace std;
  5. int main ()
  6. {
  7. int a[10]={9,12,17,30,50,20,60,65,4,49};
  8. Sort (a,a+10);
  9. For (int i=0;i<10;i++)
  10. cout<<a[i]<<";
  11. cout<<endl;
  12. return 0;
  13. }

2) Write a comparison function to implement, then call the three parameters of the Sort:sort (Begin,end,compare) becomes. For the list container, this method also applies, compare as a sort parameter, namely: Sort (Compare).

[CPP]View PlainCopy
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. Using namespace std;
  5. BOOL Compare (int A,int b)
  6. {
  7. return a<b; //default ascending order, or descending if return a>b is changed
  8. }
  9. BOOL Compare_decrease (int A,int b)
  10. {
  11. return a>b; //From a large to a small arrangement
  12. }
  13. int main ()
  14. {
  15. int a[10]={9,12,17,30,50,20,60,65,4,49};
  16. Sort (a,a+10);
  17. For (int i=0;i<10;i++)
  18. cout<<a[i]<<";
  19. cout<<endl;
  20. Sort (a,a+10,compare_decrease);
  21. For (int i=0;i<10;i++)
  22. cout<<a[i]<<";
  23. cout<<endl;
  24. return 0;
  25. }

PS: Generally not used for string sorting

Sorting algorithm accumulation (2)

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.