[Draft] STD: Sort

Source: Internet
Author: User

This chapter describes the design and use of C ++ generic algorithm sort.

In my opinion, the generic Algorithm related to sorting is a relatively complex part of C ++.

The internal implementation of sort is not fixed. In different versions of C ++, the sort algorithms used may be different, however, the worst time complexity must be O (n log n ).

GNU Standard C ++ library adopts a three-step hybrid sorting method: First, it uses a province-specific sorting (itself uses a mixture of fast sorting and heap sorting), and then insert sorting.

See: http://en.wikipedia.org/wiki/Sort_ (C % 2B % 2b)

(For more information about province sorting, see: http://zh.wikipedia.org/wiki/%E5%86%85%E7%9C%81%E6%8E%92%E5%BA%8F)

It may be because the implementation method is not fixed, and the C ++ official website does not provide relevant implementation methods.

Let's take a look at the sort description on the C ++ official website.

Http://www.cplusplus.com/reference/algorithm/sort/

(Note: The following content is my understanding of the content on the C ++ official website. If it is not accurate, please forgive me)

  • Sort function declaration:

The sort function has two forms,

One is the comparison using operator by default:

template <class RandomAccessIterator>void sort (RandomAccessIterator first, RandomAccessIterator last);

One is the form of custom comparison:

template <class RandomAccessIterator, class Compare>void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Therefore, if you want to sort a set of data of the custom type, you have two options: 1. Reload operator <; 2. Use the second form.

  • The role of the sort function:

Sort the elements in the [first, last) sequence (if the first method above is used, the elements are sorted in descending order ).

Non-stable sorting, that is, equal two elements, cannot guarantee their original order after sorting. (If you need stable sorting, use stable_sort ).

Other content (not detailed enough ):

  • Time Complexity of the sort function:

Worst time complexity O (n log n)

  • Sorting method of the sort function:

Non-stable sorting (use stable_sort if stable sorting is required ).

In different versions of C ++, the Sorting Algorithm Used by sort may be different. GNU adopts a mix of provincial sorting and insert sorting.

The following is a simple example.

#include <algorithm>#include <iostream>#include <vector>using std::cout;using std::endl;using std::vector;class Whatever{public:    Whatever(int size, char name)        : mSize(size), mName(name)    {}    int getSize() { return mSize; }    char getName() { return mName; }    bool operator< (const Whatever& what) const    {        return (this->mSize < what.mSize);    }private:    int mSize;    char mName;};void print(vector<int> vec){    for (vector<int>::iterator iter = vec.begin(); iter != vec.end(); iter++)    {        cout << *iter << ‘ ‘;    }    cout << endl;}void print(vector<Whatever> vec){    for (vector<Whatever>::iterator iter = vec.begin(); iter != vec.end(); iter++)    {        cout << iter->getSize() << iter->getName() << ‘ ‘;    }    cout << endl;}bool compare(int i, int j){    return (i > j);}bool compareWhat(Whatever i, Whatever j){    return (i < j);}int main(void){    int vecVal[] = {12, 10, 13, 15, 20, 18, 11};    vector<int> vec(vecVal, vecVal+7);    sort(vec.begin(), vec.end());    print(vec);    sort(vec.begin(), vec.end(), compare);    print(vec);    vector<Whatever> vecWhat;    for (int i = 0; i < 100; i++)    {        Whatever what(rand()%50, char(97+rand()%25));        vecWhat.push_back(what);    }    print(vecWhat);    sort(vecWhat.begin(), vecWhat.end(), compareWhat);    print(vecWhat);    return 0;}

 

[Draft] STD: Sort

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.