Detailed explanation of STL sort (sort)

Source: Internet
Author: User
Tags comparison data structures sort

0 Preface: STL, why do you have to master

For programmers, data structures are a compulsory subject. From the search to the sort, from the list to the two fork tree, almost all algorithms and principles need to understand, can not understand also to memorize. Fortunately, these theories have been more mature, the algorithm is also basically fixed, do not need to spend your mind to consider the principle of its algorithm, and no longer to verify its accuracy. However, when you start using computer language to work, you will find that in the face of different needs, you need to repeat these mature algorithms over and over again, and again and again into some of the bugs caused by their own negligence. At this point, you want to find a tool that has helped you achieve these functions, you can use them as much as you want, without affecting performance. What you need is STL, Standard Template Library!

There is a saying in the West: Don't invent the wheel again!

The STL encapsulates almost all the algorithms in the data structure, from the list to the queue, from the vector to the stack, to the hash to the two-fork tree, from the search to the sorting, from the add to the deletion ... It can be said that if you understand the STL, you will find that you do not have to adhere to the algorithm itself, so standing on the shoulders of giants to consider more advanced applications.

Sorting is one of the most widely used algorithms, this paper introduces in detail the usage and difference of different sorting algorithms in STL.

1 the sort algorithm provided by STL

C + + has been so many people like, because it has an object-oriented concept, but also maintain the high efficiency of C language features. The STL sorting algorithm also needs to be kept efficient. Therefore, for different needs, the STL provides different functions, different functions, the implementation of the algorithm is not the same.

1.1 Introduction to all sort algorithms

All the sort algorithm parameters need to be entered in a range, [begin, end]. The iterator (iterator) used here needs to be a random iterator (radomaccessiterator), that is, an iterator that can be randomly accessed, such as: It+n or something. (except partition and stable_partition)

If you need to define a comparison function yourself, you can pass the functor you have defined as a parameter. Each of these algorithms supports incoming comparison functions. The following is the list of names for all STL sort algorithm functions:

The name of the function Function description
Sort Sort all the elements of a given interval
Stable_sort Stable ordering of all elements in a given interval
Partial_sort Sort all elements in a given interval
Partial_sort_copy Copy and sort for a given interval
Nth_element Find the element that corresponds to a position in a given interval
is_sorted To determine whether an interval has been ordered.
Partition Make elements that meet a certain condition in the front
Stable_partition A relatively stable element in front of which a condition is met

Where Nth_element is the most difficult to understand, in fact, this function is used to find the first few. For example, to find the value of the number in the middle of an array that contains 7 elements, I may not care about the front or the back, and I only care about the value of the element in the fourth position.

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.