The sorting principle of the sort function

The sorting principle of the sort function

The comparison logic of the sort() method is:
First round: 1 and 5 ratio, 1 and 4 ratio, 1 and 2 ratio
Second round: 5 and 4 ratio, 5 and 2 ratio
Third round: 4 and 2 ratio

The sort function sorts all elements in a given interval. The default is ascending order, but it can also be sorted in descending order.

The time complexity of sorting by the sort function is n*log2n (note: not fast sort ===> qsort() is fast sorting), which is more efficient than sorting algorithms such as bubbling. The sort function is included in the header file as # include<algorithm> in the C++ standard library

grammar

Sort(start,end,cmp)

(1) start represents the starting address of the array to be sorted;

(2) end represents the next bit of the end address of the array;

(3) cmp is used to specify the sorting method, it can be left blank and the default is ascending.

The above is not the point, the point is the writing method of cmp():

Focus

The sort() method accepts a comparison function compare(a, b), which compares two values, and then returns a number indicating the relative order of the two values.

In this function, a defaults to the front of the array and b to the back of the array;

comp函数返回一个bool类型的值,这个值表示了在严格弱排序中(可以理解为升序排序)第一参数是否位于第二个参数之前。
也就是说如果comp返回true,则第一个参数小于第二个参数,sort根据compare的返回值将第一个参数排在第二个参数之前。
如果comp返回false,则第一个参数大于第二个参数,sort根据compare的返回值将第一个参数排在第二个参数之后。

传入A,B,定义bool myfunction (int i,int j) { return (i<j); },作为comp函数,则排列AB。
传入BA,则排列AB。
可以看出是升序排列的。(sort函数默认的comp函数也是默认升序的)

而如果我们定义bool myfunction (int i,int j) { return (i>j); },作为comp函数,
传入AB,返回false,则排列为BA,
传入BA,返回true,排列为BA。
是降序排列的。

From the above explanation, look at the following example:

int cmp(int a,int b)
{
    return a>b;//a-b;
}

In the above, if a>b; returns true, it means that a is in the front and b is in the back;

Then the result is a, b;

If in the real situation, a<b; then return false, (if it returns false, the position should be changed), indicating that a should be behind and b in front;

The result is b, a;

For ordinary arrays:

return a>b;//从高到低
return a<b;//从低到高

However, in addition to the above examples, there are some built-in type comparisons;

less<数据类型 >()//从小到大排序
greater<数据类型 >()//从大到小排序

image-20210217163526652

less<> structure

template<class _Ty = void>
    struct less
    {    // functor for operator<
    typedef _Ty first_argument_type;
    typedef _Ty second_argument_type;
    typedef bool result_type;

    constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const
        {    // apply operator< to operands
        return (_Left < _Right);
        }
    };

greater structure

template<class _Ty = void>
    struct greater
    {    // functor for operator>
    typedef _Ty first_argument_type;
    typedef _Ty second_argument_type;
    typedef bool result_type;

    constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const
        {    // apply operator> to operands
        return (_Left > _Right);
        }
    };

ht) const
{ // apply operator> to operands
return (_Left > _Right);
}
};


看不懂没关系,知道有这种类型就可以了;




For STL,

sort(a.begin(),a.end())//Need to use iterator;

Guess you like

Origin blog.csdn.net/weixin_45929885/article/details/114014786