Seven, ordering

7.1 Introduction

  • In the c++algorithm library has a powerful sorting function sort, easy to use, fast, has been able to solve most of the sort of problems we encountered in peacetime, the use of sortfunction need to include algorithms library:#include <algorithm>

7.2 sortSorting

  • Definition of Prototype:

    1. void sort (RandomAccessIterator first, RandomAccessIterator last);
    2. void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
    3. A sequence [first,last)of elements in ascending order by default interval, if a comparison function, a comparison function sort rules
    4. sortSorting 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_sortSorting

  • Definition of Prototype:
    1. void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );
    2. void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,Compare comp );
    3. A sequence [first,last)of elements in ascending order by default interval, if a comparison function, a comparison function sort rules
    4. stable_sortIt is stable sort ( the same as the elements relative positions in the sorted unchanged )

7.4 partial_sortSorting

7.4.1 the definition of Prototype:

  1. void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,RandomAccessIterator last);
  2. void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,RandomAccessIterator last, Compare comp);
  3. Rearranging the elements, so that a range [first, middle)containing a range [first, last)of ordered middle - firstsmallest elements.
  4. Range [middle, last)remaining elements in an unspecified order.
  5. Sort unstable
  6. The default with operator< 比较元素。否则用给定的二元比较函数comp to compare elements.

7.4.2 The principle

  1. Interval of the original container [first, middle)execution element make_heap()operating configuration a maximum heap
  2. Take [middle, last)each element and the firstcomparing firstelement in the stack within the maximum
  3. 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
  4. After the completion of the comparison [first, middle)element to do a sort of the sort_heap()operation, so that by increasing order.
  5. Time complexity about (last-first)log(middle-first)time applications cmp.

7.5 ordering structure

  1. sort Internal sorting function is compared with a less-than, less-than we need this type of structure can be overloaded

  2. For 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

Guess you like

Origin www.cnblogs.com/hbhszxyb/p/12232129.html