7.1 Introduction
- In the
c++
algorithm library has a powerful sorting functionsort
, easy to use, fast, has been able to solve most of the sort of problems we encountered in peacetime, the use ofsort
function need to include algorithms library:#include <algorithm>
7.2 sort
Sorting
Definition of Prototype:
void sort (RandomAccessIterator first, RandomAccessIterator last);
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- A sequence
[first,last)
of elements in ascending order by default interval, if a comparison function, a comparison function sort rules sort
Sorting is not stable ( the same as the elements relative position after sorting will occur change )
sort(begin, end, cmp)
the begin : the array to be sorted to be directed to the first pointer element
End : point to be sorted array of the last element of the next pointer to the location
cmp : parameters for the sort of guidelines, a non-default sort in descending order
for example:
#include <algorithm>//算法库 int main() { int a[5] = {1, 3, 4, 2, 5}; std::sort(a+0, a + 5); for (int i = 0; i < 5; i++) printf("%d ",a[i]); return 0; }
Cmp custom parameters, in descending order
#include <cstdio> #include <algorithm>//算法库 bool cmp(int x, int y) { return x > y;//这样是降序排序,改成return x<y;呢? } int main() { int a[5] = {1, 3, 4, 2, 5}; sort(a+0, a + 5, cmp); for (int i = 0; i < 5; i++) printf("%d ",a[i]); return 0; }
The basic data types sorted, we can also use standard library functions to sort, e.g., the above-described sort code can be changed as follows:
sort(a, a + 5, less<int>());
7.3 stable_sort
Sorting
- Definition of Prototype:
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,Compare comp );
- A sequence
[first,last)
of elements in ascending order by default interval, if a comparison function, a comparison function sort rules stable_sort
It is stable sort ( the same as the elements relative positions in the sorted unchanged )
7.4 partial_sort
Sorting
7.4.1 the definition of Prototype:
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,RandomAccessIterator last);
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,RandomAccessIterator last, Compare comp);
- Rearranging the elements, so that a range
[first, middle)
containing a range[first, last)
of orderedmiddle - first
smallest elements. - Range
[middle, last)
remaining elements in an unspecified order. - Sort unstable
- The default with
operator<
比较元素。否则用给定的二元比较函数
comp to compare elements.
7.4.2 The principle
- Interval of the original container
[first, middle)
execution elementmake_heap()
operating configuration a maximum heap - Take
[middle, last)
each element and thefirst
comparingfirst
element in the stack within the maximum - If less than the maximum value, the position of the exchange element, and the
[first, middle)
elements within the adjusted order to keep the maximum stack - After the completion of the comparison
[first, middle)
element to do a sort of thesort_heap()
operation, so that by increasing order. - Time complexity about
(last-first)log(middle-first)
time applicationscmp
.
7.5 ordering structure
sort
Internal sorting function is compared with a less-than, less-than we need this type of structure can be overloadedFor sorting structure, a method may also be overloaded comparison operators structure or the like, the above code can be modified to:
struct Stu{ int yw; int sx; bool operator <(const Stu& A) const { if (yw == A.yw) return sx > A.sx; else return yw < A.yw; } }; Stu a[1000]; sort(a+0,a+100);//对数组下标从0~99之间元素按照cmp排序规则排序
Example: noi.openjudge.cn 1.9 04: Who got the most scholarships